【问题标题】:Extract a particular line and inside a parenthesis in bash [duplicate]在bash中提取特定行并在括号内[重复]
【发布时间】:2020-10-25 04:01:55
【问题描述】:

只需要从命令的输出中提取括号内的字符串。

命令输出类似

Some sort of text I dont need
random text (text that I need to extract 123456)

我试过了,我用的是

command 123 | cut -d "(" -f2 | cut -d ")" -f1

输出结果为

Some sort of text I dont need
text that I need to extract 123456

我怎样才能输出

text that I need to extract 123456

我只需要一个管道。

TIA

【问题讨论】:

    标签: linux bash awk sed


    【解决方案1】:

    使用sed

    command 123 |  sed -n 's/.*(\([^)]*\)).*/\1/p'
    

    这会删除括号外(包括)的文本,并且仅在替换完成时打印结果,因此它会跳过不打印括号的第一行。

    【讨论】:

      猜你喜欢
      • 2016-03-24
      • 1970-01-01
      • 2022-01-18
      • 2020-04-06
      • 1970-01-01
      • 2013-05-18
      • 2017-07-25
      • 2023-03-10
      • 1970-01-01
      相关资源
      最近更新 更多