Parse URL in shell script

后端 未结 14 1736
一生所求
一生所求 2020-12-02 20:55

I have url like:

sftp://user@host.net/some/random/path

I want to extract user, host and path from this string. Any part can be random lengt

14条回答
  •  不思量自难忘°
    2020-12-02 21:51

    If you have access to Node.js:

    export MY_URI=sftp://user@host.net/some/random/path
    node -e "console.log(url.parse(process.env.MY_URI).user)"
    node -e "console.log(url.parse(process.env.MY_URI).host)"
    node -e "console.log(url.parse(process.env.MY_URI).path)"
    

    This will output:

    user
    host.net
    /some/random/path
    

提交回复
热议问题