管道grep响应第二个命令?
问题描述:
这里是我当前正在运行的命令:管道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'
有没有简单的方法可以在一行中完成这一切?
答
使用xargs
与stdin
的输出的占位符与-I{}
标志如下。 -r
标志用于确保curl
命令不会在先前的grep
输出的空输出中调用。
curl 'http://test.com/?id=12345' | grep -o -P '(?<=content="2;url=).*?(?=")' | xargs -r -I{} curl {}
有关的标志,-I
从GNU 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