我想删除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
我要删除重复的输出行。
发布于 2018-03-01 16:36:14
在某些情况下,像美汤这样的工具变得更加合适。
尝试用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" 匹配所需名称的URL将被提取,在while循环中,可能是$url已经以.jpg结尾,因此它将只被打印,而不是再次获取内容。
这种方法只是深入到1级,为了加快速度,它最终使用了&广告,并打算并行执行多个请求:
grep "\.jpg$" &需要检查&锁或等待所有后台作业完成
它以sort -u结束,返回找到的项的唯一列表。
发布于 2018-03-01 16:03:51
最好尝试:
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
...https://stackoverflow.com/questions/49053256
复制相似问题