如何提取两个括号之间的内容? [英] How can I extract the content between two brackets?

查看:45
本文介绍了如何提取两个括号之间的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的输入:

   1:FAILED      +  *1      0     (8328832,AR,UNDECLARED)

这是我所期望的:

8328832,AR,UNDECLARED

我试图找到一个通用的正则表达式,它允许去掉两个括号之间的任何内容.

I am trying to find a general regular expression that allows to take any content between two brackets out.

我的尝试是

  grep -o '\[(.*?)\]' test.txt > output.txt

但它不匹配任何东西.

推荐答案

仍在使用

Still using grep and regex

grep -oP '\(\K[^\)]+' file

\K 表示使用环顾正则表达式高级功能.更准确地说,这是一个积极的后视断言,你也可以这样做:

\K means that use look around regex advanced feature. More precisely, it's a positive look-behind assertion, you can do it like this too :

grep -oP '(?<=\()[^\)]+' file

如果你没有 -P 选项,你可以用 :

if you lack the -P option, you can do this with perl :

perl -lne '/\(\K[^\)]+/ and print $&' file

另一种更简单的方法,使用

awk -F'[()]' '{print $2}' file

这篇关于如何提取两个括号之间的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆