パターン1から2番目に現れるパターン2までのテキストを削除しますか?

パターン1から2番目に現れるパターン2までのテキストを削除しますか?

次のテキストファイルがあります。

<!--START OF FILE -->
random text
<meta> more random text </meta>
x x x x x x x 
more random text
that I dont need 
x x x x x x x

I need everything
from this point
onwards
...

次のように、<!--START OF FILE -->2番目と2番目の間のすべての項目を削除する必要があります。x x x x x x x

I need everything
from this point
onwards
...

を試しましたが、これは私が望むものではなく、sed '/<!--START OF FILE -->/,/x x x x x x x/d' test.txt最初の発生間の障壁を排除します。x x x x x x x

ベストアンサー1

これは正反対です

パターン1とパターン2の2番目の一致の間の線をどのように印刷しますか?

あなたとsed似たようなことをしてください:

sed -n '/PATTERN1/,$!{         # if not in this range
p;d                            # print and delete
}
/PATTERN2/!d                   # delete if it doesn't match PATTERN2
x;//!d                         # exchange and then, again, delete if no match
: do                           # label "do" (executed only after the 2nd match)
n;p                            # get the next line and print
b do' infile                   # go to label "do"

または1行に(gnu設定から):

sed -n '/PATTERN1/,$!{p;d;};/PATTERN2/!d;x;//!d;: do;n;p;b do' infile

もちろん、使用してカウンターする方が簡単ですawk。練習用に残しておきます...

おすすめ記事