获取文件中 grep 字符串后的所有内容
昨天出现了一种情况,有人需要我分离出文件的尾部,指定为特定字符串之后的所有内容(为了论证,“FOO”)。我需要立即执行此操作,因此选择了我知道可行的选项,而忽略了“正确方法”或“最佳方法”,并采用了以下方法:
grep -n FOO FILE.TXT | cut -f1 -d":" | xargs -I{} tail -n +{} FILE.TXT > NEWFILE.TXT
令我烦恼的是使用 xargs 作为单例值。我想我可以在这个问题上使用我的 Google-Fu,但我有兴趣看看 SO-land 的人们针对这种情况想出了什么样的东西
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这就是我马上想到的。
is what occurs to me right off.
您是否考虑过仅使用 grep 的“--after-context”参数?
像这样,这应该可以解决问题,用足够大的数字来结束文件的末尾:
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:
与上面 geekosaur 的答案类似,但此选项排除而不是包含匹配的行:
找到这个 此处 尝试上述选项后。
Similar to geekosaur's answer above, but this option excludes rather than includes the matched line:
Found this one here after trying the option above.