use*_*005 6 grep regular-expression
这是数据
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]
Run Code Online (Sandbox Code Playgroud)
我要输出
[0312122221212 ]
[KLOJ0001]
[10:43:17.8425459]
[10:43:18.4941158]
[0:0:651]
[345949]
Run Code Online (Sandbox Code Playgroud)
我尝试了很多命令,但无法使用以下命令实现结果:
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
Run Code Online (Sandbox Code Playgroud)
我得到了没有总时间的输出:
[0312122221212 ]
[KLOJ0001]
[10:43:17.8425459]
[10:43:18.4941158]
[345949]
Run Code Online (Sandbox Code Playgroud)
当我尝试这个命令时:
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
Run Code Online (Sandbox Code Playgroud)
我在输出中得到了无效值:
19/06/
[0312122221212 ]
[KLOJ0001]
[10:43:17.8425459]
[10:43:18.4941158]
[0:0:651]
[345949]
Run Code Online (Sandbox Code Playgroud)
mat*_*dev 10
你可以简单地使用这个:
grep -oP '\[.*?\]' /root/Documents/a.txt
Run Code Online (Sandbox Code Playgroud)
\[
匹配文字[
.*?
以非贪婪(惰性)方式匹配任意数量的字符。
\]
匹配文字]
现在,我将尝试解释您的正则表达式出了什么问题。
这个:
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 '\[.*?\]' /root/Documents/a.txt
Run Code Online (Sandbox Code Playgroud)
但请记住,这不是最好的方法......你原来的正则表达式的问题是你太宽容了;所以,你匹配了你不想要的东西;如果你知道某些匹配应该只包含,比如说[
,,,,和,不要使用numbers
...这是如此宽松,没有意义:更精确,例如,像这样:space
]
.{29}
[][0-9 ]{29}