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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 20:17:52  来源:igfitidea点击:

Print everything on line after match

bashgrep

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

Strangely, the accepted answer printed out the whole line, where I just wanted all the info after the target string. This worked for me:

奇怪的是,接受的答案打印了整行,我只想要目标字符串之后的所有信息。这对我有用:

sed -n 's/target_string//p' filename

Adapted from this post

改编自这篇文章

回答by geekosaur

With GNU grep, try -B0 -A999999999or 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, sedis 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