2016-12-16 14 views
0

は、私は、現在実行しているコマンドです:2番目のコマンドにパイプgrep応答?ここ

$ 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=).*?(?=")' 

応答は次のように、URLです。

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

これをすべて1行で簡単に行う方法はありますか?

答えて

0

xargsと出力先のプレースホルダをstdinから-I{}に変更して使用してください。 -rフラグは、前のgrep出力からの空の出力に対してcurlコマンドが確実に呼び出されないようにすることです。

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

フラグの小さな説明、GNU xargs manページから-I-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 
関連する問題