[英]How to delete all lines before the second match of a pattern with sed?
我有以下文件:
first
second
third
fourth
third
fifth
sixth
使用cat file | sed -n '/third/,$p'
cat file | sed -n '/third/,$p'
我可以从第一个匹配开始打印,以获得:
third
fourth
third
fifth
sixth
是否可以修改sed
命令,使其本质上忽略第一个匹配项并从第二个匹配项打印? 那将是:
third
fifth
sixth
这是一个awk
,它通过保留一个缓冲区来存储一行中出现third
行的所有行,并在再次找到third
行时重置缓冲区,来完成此操作:
awk '/third/{p=$0 RS; next} p{p=p $0 RS} END{printf "%s", p}' file
third
fifth
sixth
或者,您可以使用以下tac + awk
:
tac file | awk '1; /third/{exit}' | tac
third
fifth
sixth
与sed:
sed '1,/third/d' file | sed -n '/third/,$p'
输出:
third fifth sixth
与gnu sed
sed '/third/!d;:A;N;/\nthird/!{s/[^\n]*\n//;bA };s/[^\n]*\n//;:B;N;bB' infile
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.