删除wget输出

时间:2018-03-01 15:36:41

标签: linux terminal grep wget

我想删除wget输出中的重复行。

我使用此代码

wget -q "http://www.sawfirst.com/selena-gomez" -O -|tr ">" "\n"|grep 'selena-gomez-'|cut -d\" -f2|cut -d\# -f1|while read url;do wget -q "$url" -O -|tr ">" "\n"|grep 'name=.*content=.*jpg'|cut -d\' -f4|sort |uniq;done

像这样输出

http://www.sawfirst.com/wp-content/uploads/2018/03/Selena-Gomez-12.jpg

http://www.sawfirst.com/wp-content/uploads/2018/03/Selena-Gomez-12.jpg

http://www.sawfirst.com/wp-content/uploads/2018/03/Selena-Gomez-12.jpg

http://www.sawfirst.com/wp-content/uploads/2018/03/Selena-Gomez-12.jpg

http://www.sawfirst.com/wp-content/uploads/2018/02/Selena-Gomez-760.jpg

http://www.sawfirst.com/wp-content/uploads/2018/02/Selena-Gomez-760.jpg

我想删除重复的输出行。

2 个答案:

答案 0 :(得分:0)

更好的尝试:

mech-dump --images "http://www.sawfirst.com/selena-gomez" |
    grep -i '\.jpg$' |
    sort -u

用于Debian和衍生产品的包libwww-mechanize-perl

输出:

http://www.sawfirst.com/wp-content/uploads/2018/03/Selena-Gomez-12.jpg
http://www.sawfirst.com/wp-content/uploads/2018/02/Selena-Gomez-760.jpg
http://www.sawfirst.com/wp-content/uploads/2018/02/Selena-Gomez-404.jpg
...

答案 1 :(得分:-1)

在某些情况下,Beautiful Soup等工具会更合适。

尝试仅使用wget& grep成为一项有趣的练习,这是我天真的尝试,但我非常确定这是更好的方法

$ wget -q "http://www.sawfirst.com/selena-gomez" -O -|
grep -Eo "(http|https)://[a-zA-Z0-9./?=_-]*" |
grep -i "selena-gomez" |
while read url; do
    if [[ $url == *jpg ]]
    then
        echo $url
    else
        wget -q $url -O - |
        grep -Eo "(http|https)://[a-zA-Z0-9./?=_-]*" |
        grep -i "selena-gomez" |
        grep "\.jpg$" &
    fi
done | sort -u > selena-gomez

在第一轮:

wget -q "http://www.sawfirst.com/selena-gomez" -O -|
grep -Eo "(http|https)://[a-zA-Z0-9./?=_-]*" |
grep -i "selena-gomez" 

将提取与所需名称匹配的网址,在while循环中可能是$url已经以.jpg结尾的情况,因此只会打印而不是再次获取内容。< / p>

这种做法只是深入1级,并试图加快使用&的目的,最后打算并行执行多个请求:

grep "\.jpg$" &

需要检查&是否锁定或等待所有后台作业完成

sort -u结尾,以返回找到的唯一商品列表。

相关问题