开发者

Grep for a token outside of the last line

开发者 https://www.devze.com 2023-02-22 03:42 出处:网络
This seems like it should be easy, but I can\'t get the right syntax. I\'m trying to use grep to find all files with a certain token that have at least one line following the line with the token in it

This seems like it should be easy, but I can't get the right syntax. I'm trying to use grep to find all files with a certain token that have at least one line following the line with the token in it. So something like:

blah blah token blah
another line here

would be found by the grep, but not:

开发者_StackOverflow中文版
blah blah token blah


I'm pretty sure grep doesn't do multi-line stuff like this (though there is a -A option to print trailing context). One way would be to use head to cut off the last line first, then pipe it to grep:

for f in *; do
    echo "$f:"
    head -n -1 "$f" | grep token

Above code is not tested, I'm stuck on Windows atm.

0

精彩评论

暂无评论...
验证码 换一张
取 消