Completely new to scripting in general, and have been working on a script to go through our logs which contain entries for a variety of different things. What I'm interested in, is the entries that log user activity (logins, clicks, etc on our site).
I've pieced together the following awk script (with a ton of help) which almost works perfectly, except it prints output for lines that don't contain a username. I'd like to exclude them, and haven't been able to find out how to do that (I assume it's something simple).
Here is the code:
awk '
{ split($3, d, "@")
match($0, /"username":"[^"]*"/)
user = substr($0, RSTART + 12, RLENGTH - 13)
c[d[1] OFS user]++
}
END { for(i in c)
printf("%4d %s\n", c[i], i)
}' mycompany.log | sort -k3,3 -k2,2
I'm just not sure how to exclude all lines that do not contain username from the output.
Here is a sample of a line that contains a username:
qtp111659197-5776 - 05-26@09:37:34:240 INFO (TimingInfoProxy.java:41) - com.mycompany.api.ApiHandler-0>getUniqueDataBySource(data,{"has_values":false,"last_event_triggered":"","user_info":{"username":"[email protected]","orgid":"69d467a7-9786-47e1-9c12-bb40f9bfc65d","ip":"127.0.0.1"},"date_range":{"min_date":"","start_date":"","end_date":"","trending_start_date":"","trending_end_date":""},"terms":{"and_filtering":[]}},)
And here's a short example of a line that doesn't:
main - 05-22@10:05:21:387 INFO (ContextLoader.java:313) - Root WebApplicationContext: initialization completed in 9519 ms
"username":keystring missing, or is there an empty quoted value like"username":""? or something else? Please edit your post to include sample entries both with and without the username.