管道grep响应第二个命令?

时间:2016-12-16 05:42:42

标签: shell grep

这是我目前正在运行的命令:

curl 'http://test.com/?id=12345' | grep -o -P '(?<=content="2;url=).*?(?=")'

此命令的响应是一个URL,如下所示:

$ curl 'http://test.com/?id=12345' | grep -o -P '(?<=content="2;url=).*?(?=")'
http://google.com

我想使用任何URL来实际执行此操作:

curl 'http://test.com/?id=12345' | grep -o -P '(?<=content="2;url=).*?(?=")' | curl 'http://google.com'

有没有简单的方法可以在一行中完成这一切?

1 个答案:

答案 0 :(得分:0)

xargs与占位符一起使用,stdin的输出带有-I{}标记,如下所示。 -r标志用于确保不会在先前curl输出的空输出上调用grep命令。

curl 'http://test.com/?id=12345' | grep -o -P '(?<=content="2;url=).*?(?=")' | xargs -r -I{} curl {} 

-I页面中有关标记-rGNU xargs man的小描述,

-I replace-str
       Replace occurrences of replace-str in the initial-arguments with
       names read from standard input. 

-r, --no-run-if-empty
        If the standard input does not contain any nonblanks, do not run
        the command.  Normally, the command is run once even if there is
        no input.  This option is a GNU extension 

(或)如果您正在寻找没有其他工具的bash方法,

curl 'http://test.com/?id=12345' | grep -o -P '(?<=content="2;url=).*?(?=")' | while read line; do [ ! -z "$line" ] && curl "$line"; done
相关问题