Get everything in a file after a grep'd string

2019-04-07 17:31发布

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 -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

3条回答
何必那么认真
2楼-- · 2019-04-07 17:33

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
查看更多
对你真心纯属浪费
3楼-- · 2019-04-07 17:41
sed -n '/re/,$p' file

is what occurs to me right off.

查看更多
乱世女痞
4楼-- · 2019-04-07 17:46

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.

查看更多
登录 后发表回答