0

我有输入喜欢

/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

我需要输出(直到第一次出现 .bundle 的文本)

/System/Library/CoreServices/AOS.bundle
/System/Library/CoreServices/Br.bundle
/System/Library/CoreServices/backupd.bundle
4

5 回答 5

3

一种方法awk

$ awk '{print $1 FS}' FS='.bundle' file
/System/Library/CoreServices/AOS.bundle
/System/Library/CoreServices/Br.bundle
/System/Library/CoreServices/backupd.bundle

对于仅包含以下内容的行.bundle

$ awk '$0~FS{print $1 FS}' FS='.bundle' file
/System/Library/CoreServices/AOS.bundle
/System/Library/CoreServices/Br.bundle
/System/Library/CoreServices/backupd.bundle
于 2013-10-10T18:49:50.777 回答
1

使用sed

sed 's/\(\.bundle\).*$/\1/' Input
于 2013-10-10T18:07:35.713 回答
1

试试sed 's=bundle/.*=bundle='

于 2013-10-10T18:08:20.733 回答
1

使用egrep -o

 grep -oP '.*?\.bundle(?=/)' file

/System/Library/CoreServices/AOS.bundle
/System/Library/CoreServices/Br.bundle
/System/Library/CoreServices/backupd.bundle

工作演示:http: //ideone.com/iT6VGy

于 2013-10-10T18:12:01.943 回答
0

这是一个用于 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
❯
于 2013-10-10T18:52:14.433 回答