在shell中,我需要从URI中提取具体的查询参数。如何使用perl命令从URL读取查询参数值
我试图玩弄此得到“补偿”值
echo "/mypath/index.php?offset=20&query=uro" | perl -MURI -le 'chomp($url = <>); print URI->new($url)->query_form("offset")'
但它始终只返回offset=20&query=uro
请帮
在shell中,我需要从URI中提取具体的查询参数。如何使用perl命令从URL读取查询参数值
我试图玩弄此得到“补偿”值
echo "/mypath/index.php?offset=20&query=uro" | perl -MURI -le 'chomp($url = <>); print URI->new($url)->query_form("offset")'
但它始终只返回offset=20&query=uro
请帮
query_form
返回一个哈希,改变你的脚本:
perl -MURI -le 'chomp($url = <>); print +{URI->new($url)->query_form}->{offset}'
为了处理MULTIP乐行:
perl -MURI -nle 'print +{URI->new($_)->query_form}->{offset}'
您可以使用核心CGI
模块:
perl -MCGI=param -e 'print param("offset")' "index.php?offset=20&query=uro"
由于一个奇怪的原因,它不能与管道输入一起工作......这很重要,因为我将这个字符串解压出来 – glaz666 2012-03-01 13:39:02
@ glaz666'CGI'需要查询作为脚本的参数。你可以使用'echo“/mypath/index.php?offset=20&query=uro”| perl -MCGI = param -e'@ARGV = <>;打印参数(“偏移”)“ – 2012-03-01 14:24:04
您可以使用另外的URI::QueryParam
模块URI
.The query_param
method in the URI::QueryParam
module为您提供了查询参数值。
echo "/mypath/index.php?offset=20&query=uro" | perl -MURI -le 'use URI::QueryParam; chomp($url = <>); print URI->new($url)->query_param(offset);'
什么是打印输出的每一行'偏移'的方式,例如:我有:echo“/mypath/index.php?offset=20&query=uro \ n /mypath2/index.php?offset=30&query=uro” – glaz666 2012-03-02 13:39:18
@ glaz666:只需将'-n'选项添加到perl。看我的编辑。 – Toto 2012-03-02 14:12:10