我有一个大文本文件,中间包含一个唯一的字符串.我想要做的是使用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
Run Code Online (Sandbox Code Playgroud)
如何在target_string之后打印所有内容之前没有任何内容?
gee*_*aur 30
使用GNU grep,尝试-B0 -A999999999或类似.更好的选择可能是awk:
awk '/target_string/ {seen = 1}
seen {print}'
Run Code Online (Sandbox Code Playgroud)
如果(您的问题规格略微不清楚)您不需要打印匹配的行,sed甚至更短:
sed '1,/target_string/d'
Run Code Online (Sandbox Code Playgroud)
chi*_*ric 28
奇怪的是,接受的答案打印出整行,我只想在目标字符串后面的所有信息.这对我有用:
sed -n 's/target_string//p' filename
Run Code Online (Sandbox Code Playgroud)
改编自这篇文章
ysd*_*sdx 21
你忘记了'.':
cat textfile | grep -o "target_string.*"
Run Code Online (Sandbox Code Playgroud)
这将在每次比赛后打印所有内容,仅在同一行上:
perl -lne 'print $1 if /target_string(.*)/' textfile
Run Code Online (Sandbox Code Playgroud)
这将执行相同的操作,但它还会打印所有后续行:
perl -lne 'if ($found){print} else{if (/target_string(.*)/){print $1; $found++}}' textfile
Run Code Online (Sandbox Code Playgroud)