2016-08-30 2 views
0

私はいくつかの繰り返しパターンを含むテキストファイルを持っており、一致するパターンののペアの間の行を削除します。パターンの一致するペアの間の行を削除します

問題:「パターンライン」の最後の出現が「オープニングパターン」です。

例:

Some lines 
In the preamble 
START 
Some lines  # Remove this 
I with to remove # Remove this 
STOP    # Remove this 
Some lines 
I wish to keep 
START 
Some other lines # Remove this 
I with to remove # Remove this 
STOP    # Remove this 
Some lines 
I wish to keep 
START 
Don't remove this line 
Etc. 

だから私はものは、STARTSTOPの間のすべてを削除しないようにしたいSTART

の最後に出現した後、私が持っているかもしれないSEDとawkを持つソリューションの数を発見しました私の元のテキストが、最後の閉じたもの(例えば、hereなど)の後に開始パターンが最後に出現しなかった場合は私のために働いたが、残念ながらこれは私の問題を解決しません。

ボーナス:理想的には、閉鎖パターンを保持する行は削除したいが、開かれた行は削除したくない。これは本当に重要ではありません。なぜなら、私はいつも両方を保持し、後で閉じたものを取り除くことができるからです。

私は実際には、元々の各ファイルから最初のブックマークのみを保持するために、いくつかのブックマークがすでに含まれているいくつかの小さな文書の連結から構築された巨大なpdf文書のブックマークをクリーニングします。 これを達成するための選択肢についての提案も歓迎します。

答えて

1
$ awk '/START/,/STOP/{if($0=="START") a=""; else {a=a $0 ORS;next}} {print} END {printf "%s", a}' file 
Some lines 
In the preamble 
START 
Some lines 
I wish to keep 
START 
Some lines 
I wish to keep 
START 
Don't remove this line 
Etc. 

ウォークスルー:

/START/,/STOP/ {  # between markers 
    if($0=="START") # if START 
     a=""   # reset a and print record in the end 
    else { 
     a=a $0 ORS # build up a 
     next   # skip the print in the end 
    } 
} 
{ 
    print   # the print 
} 
END { 
    printf "%s", a # in the end print the a 
} 
関連する問題