【问题标题】:Copy line of text from input to output file while using xargs使用 xargs 时将文本行从输入复制到输出文件
【发布时间】:2019-05-08 21:03:58
【问题描述】:

我有一个工作 shell 脚本,它读取一个包含 URL 的文本文件,每个 URL 都在单独的行中。 URL 从文件中并行读取并检查其状态代码,其中将响应写入 status-codes.csv。

如何将 url-list.txt 引用的原始 URL 写入 status-codes.csv 中输出的第一列?

status-codes.sh

#!/bin/bash
xargs -n1 -P 10 curl -u user:pass -L -o /dev/null --silent --head --write-out '%{url_effective},%{http_code},%{num_redirects}\n' < url-list.txt | tee status-codes.csv

url-list.txt

http://website-url.com/path-to-page-1
http://website-url.com/path-to-page-2
http://website-url.com/path-to-page-3

status-codes.csv (当前输出)

http://website-url.com/path-to-page-2,200,1
http://website-url.com/path-to-page-after-any-redirects,200,2
http://website-url.com/404,404,2

status-codes.csv (期望的输出)

http://website-url.com/path-to-page-2,http://website-url.com/path-to-page-2,200,1
http://website-url.com/path-to-page-1,http://website-url.com/path-to-page-after-any-redirects,200,2
http://website-url.com/path-to-page-3,http://website-url.com/404,404,2

【问题讨论】:

    标签: bash xargs


    【解决方案1】:

    使用-I 选项。例如:

    xargs -n1 -P 10 -I '{}' curl -u user:pass -L -o /dev/null --silent --head --write-out '{},%{url_effective},%{http_code},%{num_redirects}\n' '{}' < url-list.txt | tee status-codes.csv
    

    man xargs:

    -I 替换-str 用从标准输入读取的名称替换初始参数中出现的 replace-str

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-05
      • 1970-01-01
      • 1970-01-01
      • 2014-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-22
      相关资源
      最近更新 更多