這裏是我的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