Awk 从字符串中提取数字

Awk 从字符串中提取数字,awk,Awk,我很难从这个数据集中提取用户号和错误。我错在哪里 来源数据: [319041185] :: [2013/08/28 08:10:22.702 P2D98 T020 d] PSComAccountsClient.UserPasswordVerify User=6272820002384270, Password=[not logged], AccessLevel=User . . [319041253] :: [2013/08/28 08:10:22.718 P2D98 T020 e] [Func

我很难从这个数据集中提取用户号和错误。我错在哪里

来源数据:

[319041185] :: [2013/08/28 08:10:22.702 P2D98 T020 d] PSComAccountsClient.UserPasswordVerify User=6272820002384270, Password=[not logged], AccessLevel=User
.
.
[319041253] :: [2013/08/28 08:10:22.718 P2D98 T020 e] [FunctorBase.Execute] (ErrorCode=Pedi.InternalError) An internal server error occurred. The account could not be found.
命令:

awk "{if (/User=/) {s=$NF; gsub (/[^0-9]/,\"\",s);} if (s==/[0=9]/ && /ErrorCode=/) {q=sub (/.*InternalError\\")"/,\"\"); } printf s; printf q}" file
电流输出:

NULL
预期产出:

6272820002384270 An internal server error occurred. The account could not be found.
比如说:

str='Source: [319041185] :: [2013/08/28 08:10:22.702 P2D98 T020 d] PSComAccountsClient.UserPasswordVerify User=6272820002384270, Password=[not logged], AccessLevel=User'
使用
grep-oP

grep -oP '(?<=User=)\d+' <<< "str"

grep-oP'(?如果文件结构一致,则使用
GNU awk
的一种方法是设置多个字段分隔符并只打印所需的字段:

$ awk -F'[=, ]' '{print $10}' file
6272820002384270
如果字段编号可以在各行之间更改,则只需在所有字段上循环:

$ awk -F'[, ]' '{for(i=1;i<=NF;i++)if($i~"User=")print substr($i,6)}' file
6272820002384270

你也可以使用grep,例如

grep -Po 'User=\K[0-9]*'

你可以用更多的细节来编辑你的帖子,不需要在评论中留下细节。你已经展示了一些示例输入和你尝试的解决方案,这是一个很好的开始,现在通过发布预期的输出来结束你的问题。我可以用以下代码来完成:awk“{if(/User=/){s=$0;sub(/.*User=/,\“\”,s);sub(/P.*/,\“\”,s);}if(s&&/ErrorCode=/){sub(/.*InternalError\\”)/,\“\”;}printf s;print}”“file”| grep“server error”| awk-F“,“{if($2~/error/){print}}”Sort | Uniq+1。除了上一个解决方案外,这些解决方案中没有任何GNU awk的具体内容,尽管其他解决方案都可以与任何现代awk一起使用。
$ awk '$1=="User"{print $2}'  RS=',? ' FS='=' file
6272820002384270
grep -Po 'User=\K[0-9]*'