从.Bundle开始捕获名字

时间:2013-10-10 18:02:01

标签: bash awk terminal grep

我输入了

/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

5 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

使用sed

sed 's/\(\.bundle\).*$/\1/' Input

答案 2 :(得分:1)

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

答案 3 :(得分: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

答案 4 :(得分: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
❯