【问题标题】:Get text between two words in bash (multilines)在bash(多行)中获取两个单词之间的文本
【发布时间】:2016-07-08 22:35:44
【问题描述】:

我正在解决这个问题。我有这段文字:

Executables:   manatee-curl
Dependencies:  base ==4.*, manatee-core >=0.1.1, dbus-client ==0.3.*,
               stm >=2.1.2.0, containers >=0.3.0.0, gtk >=0.12.0,
               text >=0.7.1.0, mtl >=1.1.0.2, old-time -any,
               old-locale -any, glib >=0.12.0, gio >=0.12.0,
               filepath >=1.1.0.3, utf8-string >=0.3.4, bytestring -any,
               network -any, curl >=1.3.7, directory -any,
               template-haskell -any, derive -any, binary -any,
               regex-tdfa -any, dbus-core -any
Cached:        No

我想在“依赖项:”和“缓存”之间获取所有这些词。 您可以将此文本视为变量,例如:

回显 $text | grep /dostuff/

要清楚,我想要得到的输出是:

base ==4.*, manatee-core >=0.1.1, dbus-client ==0.3.*,
stm >=2.1.2.0, containers >=0.3.0.0, gtk >=0.12.0,
text >=0.7.1.0, mtl >=1.1.0.2, old-time -any,
old-locale -any, glib >=0.12.0, gio >=0.12.0,
filepath >=1.1.0.3, utf8-string >=0.3.4, bytestring -any,
network -any, curl >=1.3.7, directory -any,
template-haskell -any, derive -any, binary -any,
regex-tdfa -any, dbus-core -any

谢谢。

【问题讨论】:

    标签: string bash text get between


    【解决方案1】:

    使用 GNU grep:

    echo "$text" | grep -Poz 'Dependencies:  \K(.*\n)*(?=Cached:)' | grep -Po '^ +\K.*'
    

    输出:

    基础 ==4.*,海牛核心 >=0.1.1,dbus 客户端 ==0.3.*, stm >=2.1.2.0,容器 >=0.3.0.0,gtk >=0.12.0, text >=0.7.1.0, mtl >=1.1.0.2, old-time -any, 旧语言环境 -any,glib >=0.12.0,gio >=0.12.0, 文件路径 >=1.1.0.3,utf8 字符串 >=0.3.4,字节串 -any, 网络 -any,curl >=1.3.7,目录 -any, 模板-haskell -any,派生 -any,二进制 -any, 正则表达式-tdfa -any,dbus-core -any

    见:The Stack Overflow Regular Expressions FAQ

    【讨论】:

      【解决方案2】:

      awk 来救援!

      $ awk '/^Dependencies:/{$1="";p=1} /^Cached:/{p=0} p{sub(/^ +/,"");print}' file
      
      base ==4.*, manatee-core >=0.1.1, dbus-client ==0.3.*,
      stm >=2.1.2.0, containers >=0.3.0.0, gtk >=0.12.0,
      text >=0.7.1.0, mtl >=1.1.0.2, old-time -any,
      old-locale -any, glib >=0.12.0, gio >=0.12.0,
      filepath >=1.1.0.3, utf8-string >=0.3.4, bytestring -any,
      network -any, curl >=1.3.7, directory -any,
      template-haskell -any, derive -any, binary -any,
      regex-tdfa -any, dbus-core -any
      

      您可以像往常一样分配给变量

      $ text=$(awk ...)
      

      【讨论】:

        【解决方案3】:

        您可以使用sed 获取两种模式之间的文本:

        text=$(sed -n '/^Dependencies:/,/^Cached:/{$d;s/Dependencies: *//;p;}' file)
        
        echo "$text"
        
        base ==4.*, manatee-core >=0.1.1, dbus-client ==0.3.*,
                       stm >=2.1.2.0, containers >=0.3.0.0, gtk >=0.12.0,
                       text >=0.7.1.0, mtl >=1.1.0.2, old-time -any,
                       old-locale -any, glib >=0.12.0, gio >=0.12.0,
                       filepath >=1.1.0.3, utf8-string >=0.3.4, bytestring -any,
                       network -any, curl >=1.3.7, directory -any,
                       template-haskell -any, derive -any, binary -any,
                       regex-tdfa -any, dbus-core -any
        

        IDEOne Working Demo

        【讨论】:

        • 对不起,但它不起作用,当我尝试输出 $text 时,我什么也没得到...... :(
        • @ChristopherLoen 请指定哪个sed ... linux diff 例如默认 OSX
        • 这是在 OSX sed 和 GNU sed 上测试的
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-26
        • 1970-01-01
        • 1970-01-01
        • 2017-01-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多