2010-03-10 180 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