-1

I am trying to parse a log file to extract userIds. Below is how each log is setup in catalina.out. I want to extract just the userId. How would I do that. I tried awk and grep but it returns the entire block instead of just userId. Also, I want to get every unique userId once, because the same user could log in multiple times obviously so I just want the file I write it to to just have it once. Could you guys please help me get an idea where to start? Thanks!

Here are the commands I've tried:

awk '/userId/' catalina.out

grep "userId" catalina.out

When I do this instead of returning back the userId, it returns back the entire block (as seen below)

Log format:

03:44:04.373 [127.0.0.1-8009-exec-178] 
INFO  c.c.c.x.x.w.f.AuthenticationFilter - cachObj
{"guid":"guid","userId":"userId","isPrimary":false,"accessToken":"accessToken"}
redsox2002
  • 1
  • 1
  • 1
  • 1

4 Answers4

0

I am assuming your userids will be found like "userId":"test_chumma" etc etc, if this is the case then following may help you in same.

awk -F'[":,]' '/userId/{print $11}'  Input_file

Off course if you have more requirements then kindly show us more sample output with full conditions.

Matt
  • 74,352
  • 26
  • 153
  • 180
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
0

Another example with awk:

awk -F"[:,]" '$1 ~ /^{/ {gsub("\"","",$4); print $4}' inFile.log

Here we split the records by a colon : or a comma , using the awk command's F flag -F[:,], Then if the first field in the record $1 starts with a bracket /^{/ (where we use the regex operator ~ for the condition) then we swap out the 4th field's double quotes with nothing gsub("\"","",$4) and print the result print $4

$ cat test
03:44:04.373 [127.0.0.1-8009-exec-178]
INFO  c.c.c.x.x.w.f.AuthenticationFilter - cachObj
{"guid":"guid","userId":"aUserId","isPrimary":false,"accessToken":"accessToken"}
$ awk -F"[:,]" '$1 ~ /^{/ {gsub("\"","",$4); print $4}' test
aUserId
JNevill
  • 46,980
  • 4
  • 38
  • 63
  • I unfortunately don't get back any results using this command – redsox2002 Jan 09 '17 at 15:20
  • I used your three line example log file from above. I will post the exact results of running this on my box. Perhaps your log file is formatted differently then your example? – JNevill Jan 09 '17 at 16:09
0

Here is another solution combining awk and cut from unix:

awk '{split($0,a,":"); print a[2]}' catalina.out | cut -f2 -d","
"userId"

But this will also work only for the example you posted.

JFS31
  • 518
  • 5
  • 13
0
awk -F\" '{print $6}' file 

userId
Claes Wikner
  • 1,457
  • 1
  • 9
  • 8