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

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 に答える
1

使用sed:

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

試してみてくださいsed 's=bundle/.*=bundle='

于 2013-10-10T18:08:20.733 に答える
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 に答える