这是数据
10:43:19.538281|TraceDetail |UPBSStandardGenericBillPaymentComponent.PostBillPaymentTransaction|Total Time 19/06/2019 10:43:19.538281|TraceDetail |UPBSStandardGenericBillInquiryComponent.GetBillInquiry()|Total Time |2361748 | Consumer Number [0312122221212 ] , UCID [KLOJ0001] , Sending Time [10:43:17.8425459] Receive Time [10:43:18.4941158] Total Time [0:0:651] STAN is [345949]
我要输出
[0312122221212 ]
[KLOJ0001]
[10:43:17.8425459]
[10:43:18.4941158]
[0:0:651]
[345949]
我尝试了很多命令,但无法通过以下命令获得结果:
grep -oP 'Consumer Number \K.{26}|UCID \K.{10} |Sending Time \K.{09}|Receive Time \K.{09}|Total Time \\[ \K.{8}|STAN is \K.{8}' /root/Documents/a.txt
我得到了没有总时间的输出:
[0312122221212 ]
[KLOJ0001]
[10:43:17.8425459]
[10:43:18.4941158]
[345949]
当我尝试这个命令时:
grep -oP 'Consumer Number \K.{26}|UCID \K.{10} |Sending Time \K.{09}|Receive Time \K.{09}|Total Time \K.{8}|STAN is \K.{8}' /root/Documents/a.txt
我在输出中得到无效值:
19/06/
[0312122221212 ]
[KLOJ0001]
[10:43:17.8425459]
[10:43:18.4941158]
[0:0:651]
[345949]
答案1
你可以简单地使用这个:
grep -oP '\[.*?\]' /root/Documents/a.txt
\[
匹配文字[
.*?
以非贪婪(惰性)方式匹配任意数量的字符。
\]
匹配文字]
现在,我将尝试解释您的正则表达式出了什么问题。
这个:
grep -oP 'Consumer Number \K.{26}|UCID \K.{10} |Sending Time \K.{09}|Receive Time \K.{09}|Total Time \\[ \K.{8}|STAN is \K.{8}' /root/Documents/a.txt
已损坏:grep: missing terminating ] for character class
您可能在这里粘贴得很糟糕...检查一下。
和这个:
grep -oP 'Consumer Number \K.{26}|UCID \K.{10} |Sending Time \K.{09}|Receive Time \K.{09}|Total Time \K.{8}|STAN is \K.{8}' /root/Documents/a.txt
可以修复:
grep -oP 'Consumer Number \K.{29}|UCID \K.{10} |Sending Time \K.{09}|Receive Time \K.{09}|Total Time \K[^/ ]{9}|STAN is \K.{8}' /root/Documents/a.txt
但请记住,这不是最好的方法......你原来的正则表达式的问题是你太宽容了;所以,你匹配了你不想要的东西;如果你知道某些匹配应该只包含,比如说,,,,[
和,不要使用...这是如此宽松numbers
,没有意义:更精确,例如,像这样:space
]
.{29}
[][0-9 ]{29}