Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
示例字符串:/Volumes/Macintosh HD/Users/test/
/Volumes/Macintosh HD/Users/test/
我需要输出是卷名,Macintosh HD
Macintosh HD
我尝试了以下方法:
echo "/Volumes/testVolume/test/" | sed 's/\/Volumes\/\(.*\)\//\1/g'
但输出是:testVolume/test
testVolume/test
这应该有效:
sed 's#/Volumes/\([^/]*\)/.*#\1#'
除了使用/来标记搜索和替换表达式,您也可以使用任何其他字符。#如果您的搜索或替换表达式中包含该字符,则它是常用字符/。
/
#
$ sed 's#/Volumes/\([^/]*\)/.*#\1#' <<< "/Volumes/Macintosh HD/Users/test/" Macintosh HD