2010-03-10 178 views
1

我想從許多文件中刪除段落的每個實例。我將段落的順序稱爲段落。bash:從文本文件中濾除連續行

例如:

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

的問題是,我只希望當他們出現作爲一組刪除它們。例如,如果

my first line
單獨出現,我不想刪除它。

回答

1

如果你能夠使用Perl,你可以做到這一點在這樣一行:

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 
+0

嘗試它和它的作品。謝謝。 – flybywire 2010-03-10 20:13:09

0

用sed可以做到這一點:

sed '$!N; /^\(.*\)\n\1$/!P; D' file_to_filter 
+0

這是如何使用的?我在哪裏指定過濾器? – flybywire 2010-03-10 19:46:03

+0

如果您的文件被命名爲「file_to_filter」,則回覆中的該命令將輸出您的文件,並刪除重複的行。 – 2010-03-10 20:25:18

3

@OP,我看你接受的答案,其中的段落句子是「硬性的」,所以我認爲這些段落都是ays是否一樣?這是真的,你可以使用grep。保存您想在一個文件中,例如「過濾器」,以擺脫段落,然後使用grep的-f-v選項來完成這項工作,

grep -v -f filter file