2016-11-07 44 views
2

OCCURENCES我有兩個文件:A和B.取消了所有從文件從一個文件B

A的內容:

http://example.com/1 
http://example.com/2 
http://example.com/3 
http://example.com/4 
http://example.com/5 
http://example.com/6 
http://example.com/7 
http://example.com/8 
http://example.com/9 
http://example.com/4 

內容從文件B:

http://example.com/1 
http://example.com/3 
http://example.com/9 
http://example.com/4 

現在,我想從文件中刪除A.在文件B線的所有OCCURENCES

我曾嘗試以下操作:

for LINK in $(sort -u B);do sed -i -e 's/"$LINK"//g' A; echo "Removed $LINK";done 

但它沒有做任何事情。

+0

如何關鍵是利用'sed'?你是否允許使用'sed'來編寫你的'sed'腳本?可以使用awk嗎?你提供的'grep'解決方案比使用'sed'或'awk'更簡單。 –

+0

@JonathanLeffler不,它絕對不一定是sed。 – rhillhouse

回答

4

grep -vf將會成爲這個簡單:

grep -vxFf file2 file1 

http://example.com/2 
http://example.com/5 
http://example.com/6 
http://example.com/7 
http://example.com/8 
+2

你需要一個'-x'選項或東西,以確保通過'http:// example.com/1'從文件B不刪通過'http:// example.com/100'等? –

+0

絕對@JonathanLeffler,這裏肯定需要'-x'。謝謝 – anubhava

+0

大聲笑你deffo知道這是一個重複... – 123

相關問題