1

I am having difficulty with pulling just the User Number and the Error form this dataset. Where I am going wrong?

Source data:

[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.

Command:

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

Current Output:

NULL

Intended Output:

6272820002384270 An internal server error occurred. The account could not be found.
4

3 回答 3

2

您也可以使用 grep,例如

grep -Po 'User=\K[0-9]*'
于 2013-08-28T15:36:25.543 回答
1

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

或者通过设置 的值RS

$ awk '$1=="User"{print $2}'  RS=',? ' FS='=' file
6272820002384270
于 2013-08-28T15:36:13.163 回答
0

比方说:

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"

使用 awk:

awk -F'[,=]+' '{print $2}' <<< "str"
于 2013-08-28T15:36:03.223 回答