请在bash中帮助编写下面一行的正则表达式。我的文件中有这样的多行代码,我只想从文件中捕获以下值。我怎样才能做到呢?
被提取的价值-
Y324256
vmigha3
idea注-要提取的值总是字母数字。左边和右边的方括号总是有我不想提取的数值。
文件看起来是这样的-
2021-05-13 15:35:31,804 [16] [Y324256] [341745] DEBUG Server - End Webservice method GetProcessStatus
2021-05-13 15:35:32,587 [11] [vmigha3] [341749] DEBUG Domain - Reading user permissions from the database
2022-03-03 09:08:10,699 [31] [idea] [80387] INFO Server - Begin Webservice call
2022-04-06 01:18:33,822 [MonitorThread] INFO - Reading user permissions from the database
2022-04-06 01:18:33,845 [None] DEBUG -Begin Webservice call码
grep -oP '\[\K\d*[A-Za-z][\dA-Za-z]*(?=])' file > output_file但这给了我下面的结果-
Y324256
vmigha3
idea
MonitorThread
None我想知道是否可以先找到以2022-04-06 01:18:33,845开始的行(这个日期格式),然后捕获第4条记录。
发布于 2022-04-29 15:21:36
假设/谅解:
如果该行中有3x组[...]
[...]的内容,跳过行。
awk的一个想法是使用]和[的双字段分隔符(不需要正则表达式):
$ awk -F'[][]' 'NF>6 {print $4}' file
Y324256
vmigha3
idea发布于 2022-04-29 14:55:38
必须是grep吗?我是说:
cut -d[ -f3 file | cut -d] -f1如果必须的话,那么只需匹配括号。
grep -oP '^[^[]*\[[^[]*\[\K[^]]*'https://stackoverflow.com/questions/72054925
复制相似问题