-1

のパターンを見るたびに; そして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]

ありがとう。

4

3 に答える 3

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. 
于 2013-05-24T19:28:01.120 に答える
0

GNU Grep の使用

質問からの入力を/tmp/corpusとして保存すると、次のようにコンテキスト行を除外することで正しい出力が得られます。

{ egrep -A1 ';|abc\[0\]' | egrep -v ';|^--'; } < /tmp/corpus

GNU Grep を使用しているが、シェルが Bash でないか、上記のリダイレクトをサポートしていない場合、次のパイプラインは同等ですが (私の意見では) 読みにくくなります。

egrep -A1 ';|abc\[0\]' /tmp/corpus | egrep -v ';|^--'
于 2013-05-24T20:11:32.463 に答える
0

純粋な GNU sed

sed -n '/;/ {:kn;h; // bk}; /abc\[0\]/ {x ;//! {p;x;p;x}}' ファイル

xyz 何とか何とか、
abc[0]
pqr 何とか何とか
abc[0]
于 2013-05-24T20:41:05.260 に答える