这里是我的Bash代码:如何在`/`之后获取这个字符串的部分?
echo "Some string/Another string" | grep -o "\/.*"
这将返回/Another string
。
但我不希望/
包含在由echo
返回的值中。
如何更改正则表达式来完成此操作?
编辑:我想匹配/
后面的所有内容,不管后面是什么。 "Another string"
并不总是在/
之后。
这里是我的Bash代码:如何在`/`之后获取这个字符串的部分?
echo "Some string/Another string" | grep -o "\/.*"
这将返回/Another string
。
但我不希望/
包含在由echo
返回的值中。
如何更改正则表达式来完成此操作?
编辑:我想匹配/
后面的所有内容,不管后面是什么。 "Another string"
并不总是在/
之后。
如果你有GNU grep的支持PCRE那么你可以使用\K
忘记了比赛。
$ echo "Some string/Another string" | grep -oP "\/\K.*"
Another string
随着参数扩展:
$ string='Some string/Another string'
$ echo "${string#*/}"
Another string
与#
扩展删除什么从扩展参数开始后说到。
随着AWK:
$ awk -F/ '{print $2}' <<< "$string"
Another string
这设置字段分隔符/
并打印所述第二场。
为此,您可以用切命令:
如果你想的/
cut -d '/' -f 2 <<< "Some string/Another string/abc"
output: Another string
第一和第二出现之间的字符串如果你想的/
cut -d '/' -f 2- <<< "Some string/Another string/abc"
output: Another string/abc
第一次出现后,整个字符串
详细说明'\ K'只是让左手边看起来很方便:'(?<\ /)。*' – andlrc