2016-12-16 138 views
0

这里是我当前正在运行的命令:管道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' 

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

回答

0

使用xargsstdin的输出的占位符与-I{}标志如下。 -r标志用于确保curl命令不会在先前的grep输出的空输出中调用。

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

有关的标志,-IGNU xargs man-r一个小说明,

-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