这是一个用于 python 的正则表达式匹配,带有组:http ://regex101.com/r/yL3xZ5
这是一个简单的例子awk
❯ awk '/\.bundle/{ gsub(/\.bundle\/.*$/, ".bundle"); print; }' <<_EOF_
/System/Library/CoreServices/AOS.bundle/Contents/version.plist
/System/Library/CoreServices/Br.bundle/Contents/Resources/brtool
/System/Library/CoreServices/backupd.bundle/Contents/PlugIns/brtools.bundles/Contents/Version.plist
_EOF_
/System/Library/CoreServices/AOS.bundle
/System/Library/CoreServices/Br.bundle
/System/Library/CoreServices/backupd.bundle
❯
另一个在sed
:
❯ sed -e 's@\(\.bundle\)/.*$@\1@' <<_EOF_
/System/Library/CoreServices/AOS.bundle/Contents/version.plist
/System/Library/CoreServices/Br.bundle/Contents/Resources/brtool
/System/Library/CoreServices/backupd.bundle/Contents/PlugIns/brtools.bundles/Contents/Version.plist
_EOF_
/System/Library/CoreServices/AOS.bundle
/System/Library/CoreServices/Br.bundle
/System/Library/CoreServices/backupd.bundle
❯
另一个纯粹的bash
:
❯ while read line; do echo ${line/.bundle\/*/.bundle}; done <<_EOF_
/System/Library/CoreServices/AOS.bundle/Contents/version.plist
/System/Library/CoreServices/Br.bundle/Contents/Resources/brtool
/System/Library/CoreServices/backupd.bundle/Contents/PlugIns/brtools.bundles/Contents/Version.plist
_EOF_
/System/Library/CoreServices/AOS.bundle
/System/Library/CoreServices/Br.bundle
/System/Library/CoreServices/backupd.bundle
❯
最后,与grep
❯ grep -oP '^(.*?\.bundle)(?=/)' <<_EOF_
/System/Library/CoreServices/AOS.bundle/Contents/version.plist
/System/Library/CoreServices/Br.bundle/Contents/Resources/brtool
/System/Library/CoreServices/backupd.bundle/Contents/PlugIns/brtools.bundles/Contents/Version.plist
_EOF_
/System/Library/CoreServices/AOS.bundle
/System/Library/CoreServices/Br.bundle
/System/Library/CoreServices/backupd.bundle
❯