2013-02-26 65 views
0

我有一個很大的文本文件,我想從中刪除一些文本文件中的某些行。看起來Unix shell中的sed命令是一個很好的方法。但是,我一直無法弄清楚使用哪個標誌。 。使用sed刪除txt文件中的行

database.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 

回答

6

grepsed這更適合:

grep -Fxv -f lines_to_remove.txt database.txt > what_i_really_really_want.txt 
+0

您確定該命令有效嗎?我得到一個'grep:無效的後退引用'。 – nunos 2013-02-26 16:01:26

+0

您的lines_to_remove.txt文件必須包含一些特殊字符。嘗試添加'-F'。編輯... – 2013-02-26 16:02:16

+0

將無法​​正常工作,如果database.txt有類似這是第10行。你應該添加'-x'來匹配整行。 – dogbane 2013-02-26 16:02:55

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

+1的解決方案,但我建議你不要使用字母'l'(el)作爲變量名,因爲它看起來太像數字'1'(一),甚至在某些字體中難以區分,因此混淆你的代碼。字母「O」(哦)與數字「0」(零)同上。 – 2013-02-26 16:51:19

1

我會用comm此:

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

的命令是更適合您的需求。

注意<(commmand)語法是一種雙擊語法,因此在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