我如何在另一个文件中写入 URL 和相关的 HTTP 状态?

我如何在另一个文件中写入 URL 和相关的 HTTP 状态?

我有一个包含网站 URL 的文件。我正在尝试编写一个 bash 脚本,该脚本将获取任何包含 URL 的文件并写入另一个文件 - URL 和相关状态代码。
我试图实现的是:
我的文件中有 URL,如下所示:

http://google.com
http://trafficinviter.com
http://stackoverflow.com   
............
............
Upto 30 crore lines of URL in a file.   

我正在使用cURL命令获取这些 URL 的 Http 状态。
因此,我不明白如何获取 URL 及其对应的 HTTP 状态。
假设我需要将输出放在如下文件中:

    http://google.com
    200 ok    
    http://trafficinviter.com
    200 ok
    http://stackoverflow.com   
    200 ok
    http://example.com
    404 
    ............
    ............
    Upto 30 crore lines of URL in a file. 

另外,我正在寻找一个可以让我快速轻松地完成工作的 bash。由于行数太多,我无法找到合适的解决方案。请帮帮我。非常感谢。

答案1

尝试:

while read url
do
    echo "$url"
    curl -sI "$url" | head -1
done < list-of-urls > output-file

这只是从文件中读取每一行输入list-of-urls,将其输出回来,然后从该 URL 获取第一行输出curl -I(即 HTTP 响应状态)。整个循环的输出转到output-file

要同时测试多个 URL,请使用GNU并行

GNU parallel makes sure output from the commands is the same output as
you would get had you run the commands sequentially. This makes it
possible to use output from GNU parallel as input for other programs.

例如:

parallel -a list-of-urls 'echo {}; curl -sI {} | head -1' > output-file

但是,不能保证输出一定有序:

$ cat output-file
http://example.com
HTTP/1.1 200 OK
http://google.com
HTTP/1.1 302 Moved Temporarily
http://stackoverflow.com
HTTP/1.1 200 OK
http://trafficinviter.com
HTTP/1.1 200 OK

答案2

使用 GNU Parallel 你可以做类似的事情:

cat input | parallel -k -j 100 --tag 'curl -sI {} | head -1'

相关内容