【问题标题】:remove duplicate lines in wget output删除 wget 输出中的重复行
【发布时间】:2018-03-01 15:36:41
【问题描述】:

我想删除 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

我想删除重复的输出行。

【问题讨论】:

    标签: linux terminal grep wget


    【解决方案1】:

    最好试试:

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

    软件包 libwww-mechanize-perl 用于 Debian 及其衍生产品。

    输出:

    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
    ...
    

    【讨论】:

      【解决方案2】:

      在某些情况下,Beautiful Soup 之类的工具会变得更合适。

      尝试仅使用 wgetgrep 来做这件事会变成一个有趣的练习,这是我天真的尝试,但我很确定有更好的方法来做这件事

      $ 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 结尾以返回找到的唯一项目列表。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-10-15
        • 2015-08-12
        • 1970-01-01
        • 2021-06-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多