我想从许多文件中删除段落的每个实例。我称段落为一系列行。
例如:
我的第一行 我的第二行 我的第三行 第四个 第 5 次也是最后一次
问题是我只想在它们作为一个组出现时删除它们。例如,如果
我的第一行单独出现我不想删除它。
我想从许多文件中删除段落的每个实例。我称段落为一系列行。
例如:
我的第一行 我的第二行 我的第三行 第四个 第 5 次也是最后一次
问题是我只想在它们作为一个组出现时删除它们。例如,如果
我的第一行单独出现我不想删除它。
@OP,我看到你接受了你的段落句子是“硬编码”的答案,所以我认为这些段落总是相同的?这是真的,你可以使用grep
. 将要删除的段落存储在文件中,例如“过滤器”,然后使用grep-f
的-v
选项来完成这项工作,
grep -v -f filter file
如果你能够使用 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 以段落模式 slurp 文件。值 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
你可以用 sed 做到这一点:
sed '$!N; /^\(.*\)\n\1$/!P; D' file_to_filter