bash 匹配后在线打印所有内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5346896/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Print everything on line after match
提问by twk789
I have an large textfile that contains an unique string in the middle. What i want to do is to print everything AFTER the string by using grep.
我有一个大文本文件,中间包含一个唯一的字符串。我想要做的是使用 grep 在字符串之后打印所有内容。
cat textfile | grep "target_string"
This highlights target_string but prints the whole file
cat textfile | grep -o "target_string"
This prints only target_string
cat textfile | grep -o "target_string*"
This prints only target_string
How can i print everything after target_string and nothing before?
如何在 target_string 之后打印所有内容而之前没有任何内容?
采纳答案by ysdx
You forgot the '.':
你忘记了“.”:
cat textfile | grep -o "target_string.*"
回答by chimeric
回答by geekosaur
With GNU grep
, try -B0 -A999999999
or similar. A better choice might be awk
:
使用 GNU grep
,尝试-B0 -A999999999
或类似。更好的选择可能是awk
:
awk '/target_string/ {seen = 1}
seen {print}'
If (your problem specification is slightly unclear) you don't also need to print the matching line, sed
is even shorter:
如果(你的问题说明有点不清楚)你也不需要打印匹配的行,sed
甚至更短:
sed '1,/target_string/d'
回答by Chris Koknat
This will print everything after each match, on that same line only:
这将在每次匹配后打印所有内容,仅在同一行上:
perl -lne 'print if /target_string(.*)/' textfile
This will do the same, except it will also print all subsequent lines:
这将执行相同的操作,除了它还会打印所有后续行:
perl -lne 'if ($found){print} else{if (/target_string(.*)/){print ; $found++}}' textfile