2017-09-28 52 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 –