Extract filename and path from URL in bash script

后端 未结 13 968
别跟我提以往
别跟我提以往 2021-01-30 14:17

In my bash script I need to extract just the path from the given URL. For example, from the variable containing string:

http://login:password@example.com/one/more/dir/fi

13条回答
  •  北恋
    北恋 (楼主)
    2021-01-30 14:40

    gawk

    echo "http://login:password@example.com/one/more/dir/file.exe?a=sth&b=sth" | awk -F"/" '
    {
     $1=$2=$3=""
     gsub(/\?.*/,"",$NF)
     print substr($0,3)
    }' OFS="/"
    

    output

    # ./test.sh
    /one/more/dir/file.exe
    

提交回复
热议问题