仅提取第一个路径的域名、端口号

仅提取第一个路径的域名、端口号
str1="https://111.111.111.111:2222/cpsess8993738132/scripts4/listaccts/"
str2="https://111.111.111.111:2222/cpsess8993738132/"
str3="https://111.111.111.111:2222/cpsess8993738132/cgi/server/xyz.cgi|Server     Security & Firewall|111.22.33.31.|0|1|0||0|2221331130315786|fzerstxBjUiZ"

我怎样才能只提取到https://IP:port/firstpath/其余/of/the/paths/and_some_thing_else

我只想提取最多https://IP:端口/第一路径/

我怎样才能在shell脚本中做到这一点?

答案1

> str1="https://111.111.111.111:2222/cpsess8993738132/scripts4/listaccts/"
> rest="${str1#*//?*/}"
> str="${str1%"$rest"}"
> echo "$str"
https://111.111.111.111:2222/

答案2

我从昨天晚上开始就在尝试,在 StackExchange 上发布后几分钟内我找到了解决方案。

string="https://111.22.33.44:5557/xyzsess81122334442/scripts4/listaccts/xfercpanel/some" 感谢您的帮助...

echo $string | sed 's/\//\/\n/4'


>     echo $string | sed 's/\//\/\n/4'
>        s/ -> replace command
>       \/ => find all /
>       replace with \/\n -> \and newline 
>      /4 --> 4th occurance

相关内容