user2726046
user2726046

Reputation: 11

Pulling a number from a string

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.

Upvotes: 1

Views: 74

Answers (3)

Chris Seymour
Chris Seymour

Reputation: 85785

One approach using GNU awk if the file structure is consistent is to set multiple field separators and just print the field you need:

$ awk -F'[=, ]' '{print $10}' file
6272820002384270

If the field number could change from line to line the just loop over all the fields:

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

Alternately by setting the value of RS:

$ awk '$1=="User"{print $2}'  RS=',? ' FS='=' file
6272820002384270

Upvotes: 1

Adrian Fr&#252;hwirth
Adrian Fr&#252;hwirth

Reputation: 45576

You could also use grep, e.g.

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

Upvotes: 2

anubhava
anubhava

Reputation: 785108

Let's say:

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

Using grep -oP:

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

Using awk:

awk -F'[,=]+' '{print $2}' <<< "str"

Upvotes: 0

Related Questions