1

多くのファイルから段落の各インスタンスを削除したい。私は段落を一連の行と呼びます。

例えば:

私の最初の行
私の二行目
私の三行目
第4
5番目と最後

問題は、それらがグループとして表示されたときにのみ削除したいということです。たとえば、

私の最初の行
単独で表示されます 削除したくありません。

4

3 に答える 3

1

Perlを使用できる場合は、次のように1行で実行できます。

perl -0777 -pe 's/my first line\nmy second line\nmy third line\nthe fourth\n5th and last\n//g' paragraph_file

説明はperlrunにあります:

特別な値00を指定すると、Perlは段落モードでファイルを丸呑みします。値0777は、その値を持つ有効なバイトがないため、Perlがファイル全体を丸呑みする原因になります。

サンプル入力:

my first line
my second line
my third line
the fourth
5th and last
hey
my first line
my second line
my third line
the fourth
5th and last

hello
my first line

出力:

$ perl -0777 -pe 's/my first line\nmy second line\nmy third line
\nthe fourth\n5th and last\n//g' paragraph_file
hey

hello
my first line
于 2010-03-10T19:34:21.487 に答える
0

あなたはsedでそれを行うことができます:

sed '$!N; /^\(.*\)\n\1$/!P; D' file_to_filter
于 2010-03-10T19:37:32.687 に答える