开发者

Get everything in a file after a grep'd string

Yesterday a situation came up where someone needed me to separate out the tail end of a file, specified as being everything after a particular string (for sake of argument, "FOO"). I needed to do this immediately so went with the option that I knew would work and disregarded The Right Way or The Best Way, and went with the following:

grep -n FOO FILE.TXT | cut -f1开发者_C百科 -d":" | xargs -I{} tail -n +{} FILE.TXT > NEWFILE.TXT

The thing that bugged me about this was the use of xargs for a singleton value. I thought that I could go flex my Google-Fu on this but was interested to see what sort of things people out in SO-land came up with for this situation


sed -n '/re/,$p' file

is what occurs to me right off.


Did you consider just using grep's '--after-context' argument?

Something like, this should do the trick, with a sufficiently large number to tail out the end of the file:

grep --after-context=999999 -n FOO FILE.TXT > NEWFILE.TXT


Similar to geekosaur's answer above, but this option excludes rather than includes the matched line:

sed '1,/regex/d' myfile

Found this one here after trying the option above.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜