如何在shell脚本中解析URL参数

use*_*054 2 shell awk

我已经在 python 中成功尝试了相同的方法来解析 URL 参数,但我想知道是否有办法在 Shell 脚本中执行相同的操作。

假设我有一个 URL 值:

http://www.abcdsample.com/listservices?a=1&b=10&c=abcdeeff&d=1663889&listservices=a|b

所需输出:

URL: http://www.abcdsample.com/
Service: listservices
a=1
b=10
c=abcdeeff
d=1663889
listservices=a|b
Run Code Online (Sandbox Code Playgroud)

Chr*_*our 5

这是一种方法:

BEGIN{
    FS="?"
}
{
    url=$1
    sub(/[^/]*$/,"",url)
    print "URL:",url

    sub(/.*[/]/,"",$1)
    print "Service:",$1

    n=split($2,b,/&/)
    for (i=1;i<=n;i++)
        print b[i]
}
Run Code Online (Sandbox Code Playgroud)

将其另存为script.awk并运行awk -f script.awk file

URL: http://www.abcdsample.com/
Service: listservices
a=1
b=10
c=abcdeeff
d=1663889
listservices=a|b
Run Code Online (Sandbox Code Playgroud)

注意:这适用于如下 URL: