Extract filename and path from URL in bash script

后端 未结 13 1219
别跟我提以往
别跟我提以往 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条回答
  •  Happy的楠姐
    2021-01-30 14:24

    If you have a gawk:

    $ echo 'http://login:password@example.com/one/more/dir/file.exe?a=sth&b=sth' | \
      gawk '$0=gensub(/http:\/\/[^/]+(\/[^?]+)\?.*/,"\\1",1)'
    

    or

    $ echo 'http://login:password@example.com/one/more/dir/file.exe?a=sth&b=sth' | \
      gawk -F'(http://[^/]+|?)' '$0=$2'
    

    Gnu awk can use regular expression as field separators(FS).

提交回复
热议问题