每当它看到一个模式;和abc [0]我需要在之后打印第一行;和一行包含abc [0]。
我有类似的东西
blah blah;
blah blah blah;
xyz blah blah,
blah blah
abc[2]
abc[1],
abc[0]
blah blah,
blah blah
abc[1],
abc[0]
blah blah
blah blah;
pqr blah blah
blah blah blah
abc[0]
所需输出如下所示
xyz blah blah,
abc[0]
pqr blah blah
abc[0]
感谢。
答案 0 :(得分:1)
awk '/;/ { f=1; next } f{ print $0 ; f=0; next} /abc\[0\]/ { print }' inputfile
/;/ { f=1; next } - Set the flag to 1 when you encounter a line with `;` pattern.
Since I believe you want to print the line after the `;` and not one with the `;`
you do next to skip the entire pattern action statements
f{ print $0 ; f=0; next} - If the flag is true, you print the line, set the flag to false
and skip the rest.
/abc\[0\]/ { print } - If you find the second pattern you print it.
答案 1 :(得分:0)
如果您的问题输入存储为 / tmp / corpus ,以下内容将通过过滤掉上下文行来提供正确的输出。
{ egrep -A1 ';|abc\[0\]' | egrep -v ';|^--'; } < /tmp/corpus
如果您有GNU Grep,但是您的shell不是Bash或者不支持上面的重定向,则以下管道是等效的,但(在我看来)可读性较差:
egrep -A1 ';|abc\[0\]' /tmp/corpus | egrep -v ';|^--'
答案 2 :(得分:0)
sed -n '/;/ {:k n;h; // bk}; /abc\[0\]/ {x ;//! {p;x;p;x}}' file xyz blah blah, abc[0] pqr blah blah abc[0]