0

我有一个大文本文件,我想从中删除另一个文本文件中的一些行。似乎sedUnix shell 中的命令是执行此操作的好方法。但是,我无法弄清楚为此使用哪些标志。.

数据库.txt:

this is line 1
this is line 2
this is line 3
this is line 4
this is line 5

lines_to_remove.txt

this is line 1
this is line 3

what_i_want.txt

this is line 2
this is line 4
this is line 5
4

3 回答 3

6

grepsed这个更适合:

grep -Fxv -f lines_to_remove.txt database.txt > what_i_really_really_want.txt
于 2013-02-26T15:56:35.290 回答
1

awk

$ awk 'NR==FNR{a[$0];next}!($0 in a)' remove.txt database.txt
this is line 2
this is line 4
this is line 5

$ awk 'NR==FNR{a[$0];next}!($0 in a)' remove.txt database.txt > output.txt
于 2013-02-26T16:07:22.013 回答
1

我会用comm这个:

comm -1 <(sort database.txt) <(sort lines_to_remove.txt) > what_i_want.txt

该命令更适合您的需求。

注意<(commmand)语法是一种 bashism,因此在 SO 上受到很大的诽谤。它是以下内容的简写:

sort database.txt > sorted_database.txt
sort lines_to_remove.txt > sorted_lines_to_remove.txt
comm -1 sorted_database.txt sorted_lines_to_remove.txt > what_i_want.txt
于 2013-02-26T16:16:30.373 回答