2016-12-16 134 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