【问题标题】:Capture The First Name Begin With .Bundle [closed]捕获以 .Bundle 开头的名字 [关闭]
【发布时间】:2013-10-18 15:17:18
【问题描述】:

我有类似的输入

/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

【问题讨论】:

  • 我没有这样的输入。现在呢?

标签: bash awk terminal grep


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • \.bundle 将包括请求的句号
    【解决方案3】:

    使用sed

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

    【讨论】:

      【解决方案4】:

      使用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

      【讨论】:

      • @Downvoter:我可以知道为什么它被否决了吗?
      • 我没有投反对票,但你的 .* 在我的测试中太贪心了。最后一行:/System/Library/CoreServices/backupd.bundle/Contents/PlugIns/brtools.bundle
      • @svante:它适用于我的 OSX,这就是我发布此内容的原因。随时可以制作:egrep -o '^.+?\.bundle' file
      • 我也试过了,没有运气(Ubuntu 13.04 上的 egrep 2.14)。作品:grep -oP '^.+?\.bundle' Input
      • @svante:我的 Linux 显示 GNU grep 2.6.3 并且两个命令(.* 和 .+?)也可以在 Linux 上运行。
      【解决方案5】:

      这是一个正则表达式匹配,带有组,用于 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
      ❯
      

      【讨论】:

        猜你喜欢
        • 2012-04-25
        • 2014-02-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多