2017-09-28 93 views
1

我有兩個文件,一個包含大約100個根域,另一個文件僅包含URL。現在我必須過濾該URL列表以獲取第三個文件,其中只包含具有列表中的域的URL。 URL列表bash中的foreach循環

例子:

github.com 
youtube.com 
facebook.com 

Resut:

| URL       | 
| ------------------------------| 
| http://github.com/name  | 
| http://stackoverflow.com/name2| 
| http://stackoverflow.com/name3| 
| http://www.linkedin.com/name3 | 

字表舉例

| http://github.com/name  | 

我的目標是過濾掉整排的其中URL包含特定單詞。這是我試過的:

for i in $(cat domains.csv); 
do grep "$i" urls.csv >> filtered.csv ; 
done 

結果很奇怪,我有一些鏈接,但不是所有鏈接都包含第一個文件的根域。然後我試着用python做同樣的事情,看到bash沒有做我想做的事情,我用python腳本得到了更好的結果,但是編寫python腳本比運行bash命令需要更多的時間。

我該如何完成這與bash在進一步?

+2

你想用'bash'處理這樣的文本文件?你可以單獨使用'grep'來做這件事。 – Inian

+0

當我嘗試這個:grep「github」urls.csv> github.com 我有所有的github網址,所以我認爲我在做每個循環的錯誤 –

+0

@Spopic:[你可以標記答案爲接受通過點擊此答案左上角的刻度標記](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work/5235#5235) –

回答

4

使用grep

grep -F -f domains.csv url.csv 

測試結果:

$ cat wordlist 
github.com 
youtube.com 
facebook.com 

$ cat urllist 
| URL       | 
| ------------------------------| 
| http://github.com/name  | 
| http://stackoverflow.com/name2| 
| http://stackoverflow.com/name3| 
| http://www.linkedin.com/name3 | 

$ grep -F -f wordlist urllist 
| http://github.com/name  | 
+0

您應該使用' - F'標誌也將字符串視爲文字而不是正則表達式 – Inian

+0

'grep -Fxf domains.csv <(cut -d'[|]' codeforester

+0

@Inian考慮了你的建議並添加了,但仍然沒有'-F'它給出了OP想要的o/p –