LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 06-26-2018, 08:00 PM   #1
oliveoyl
Member
 
Registered: Sep 2016
Posts: 42

Rep: Reputation: Disabled
Question Help with command/script to analyze syslog


I have a bunch of files organized by host/date and I need help coming up with some useful stats for it. I'm interested getting a count of the module per user for all the files. I've been looking into awk/sed to do this but if you have any other ideas please share.

[root@logger ]# grep ModuleUsageTracking /var/log/syslog/login*/2018/06/26/messages | awk '{print $6}' | tail
user=bpt,module=gcc/5.2.0,path=/apps/modulefiles/Core/gcc/5.2.0,host=login02,job=none
user=bpt,module=python/3.5.0,path=/apps/modulefiles/Compiler/gcc/5.2/python/3.5.0,host=login02,job=none
user=bpt,module=gcc/5.2.0,path=/apps/modulefiles/Core/gcc/5.2.0,host=login02,job=none
user=bpt,module=python/3.5.0,path=/apps/modulefiles/Compiler/gcc/5.2/python/3.5.0,host=login02,job=none
user=bpt,module=gcc/5.2.0,path=/apps/modulefiles/Core/gcc/5.2.0,host=login02,job=none
user=bpt,module=python/3.5.0,path=/apps/modulefiles/Compiler/gcc/5.2/python/3.5.0,host=login02,job=none
user=zhl,module=gcc/4.8.2,path=/apps/modulefiles/Core/gcc/4.8.2,host=login03,job=none
user=zhl,module=cmake/3.5.2,path=/apps/modulefiles/Compiler/gcc/4.8.2/cmake/3.5.2,host=login03,job=none
user=rew,module=gcc/4.8.2,path=/apps/modulefiles/Core/gcc/4.8.2,host=login03,job=none
user=rew,module=cmake/3.5.2,path=/apps/modulefiles/Compiler/gcc/4.8.2/cmake/3.5.2,host=login03,job=none

Last edited by oliveoyl; 06-26-2018 at 08:25 PM.
 
Old 06-26-2018, 08:18 PM   #2
oliveoyl
Member
 
Registered: Sep 2016
Posts: 42

Original Poster
Rep: Reputation: Disabled
Looking to get something like this:

user count module

bpt 3 gcc-5.2.0
bpt 3 python-3.5.0
zhl 1 gcc-4.8.2
zhl 1 cmake-3.5.2
.
.
.
 
Old 06-26-2018, 09:06 PM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,128

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Perl or awk would be my preferred - but any you favour would work; python, go, whatever. Hell, you could even use C ...

Given that you use grep piped to awk, I'm guessing your awk is not strong; you can do the selection using regex in awk itself. It also has substringing, but I'd use its ability to define multiple field separators to do the leg work. Then you can easily work on fields. Try this and see if it gets you any further.
Code:
awk -F"[/=,]" '{print $2"\t"$4"-"$5}'
.FWIW I used a one-liner with arrays of arrays (a gawk extension) to produce the following
Code:
User	Count	Module
zhl	1	 gcc-4.8.2
zhl	1	 cmake-3.5.2
bpt	3	 python-3.5.0
bpt	1	 python-2.4.0
bpt	3	 gcc-5.2.0
rew	1	 gcc-4.8.2
rew	1	 cmake-3.5.2

Last edited by syg00; 06-26-2018 at 09:08 PM. Reason: removed \t, added -
 
1 members found this post helpful.
Old 06-27-2018, 08:47 AM   #4
oliveoyl
Member
 
Registered: Sep 2016
Posts: 42

Original Poster
Rep: Reputation: Disabled
Thanks syg00. I ended up with a long awk | awk -F, | sed | sort | uniq command. Can you educate me on your awk field separator -F"[/=,]" ? It works but also returns a bunch of blank output with just -



Quote:
Originally Posted by syg00 View Post
Perl or awk would be my preferred - but any you favour would work; python, go, whatever. Hell, you could even use C ...

Given that you use grep piped to awk, I'm guessing your awk is not strong; you can do the selection using regex in awk itself. It also has substringing, but I'd use its ability to define multiple field separators to do the leg work. Then you can easily work on fields. Try this and see if it gets you any further.
Code:
awk -F"[/=,]" '{print $2"\t"$4"-"$5}'
.FWIW I used a one-liner with arrays of arrays (a gawk extension) to produce the following
Code:
User	Count	Module
zhl	1	 gcc-4.8.2
zhl	1	 cmake-3.5.2
bpt	3	 python-3.5.0
bpt	1	 python-2.4.0
bpt	3	 gcc-5.2.0
rew	1	 gcc-4.8.2
rew	1	 cmake-3.5.2
 
Old 06-27-2018, 02:34 PM   #5
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,794

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
Thefollowing is based on the previous posts (untested)
Code:
awk '/ModuleUsageTracking/ {print $6}' /var/log/syslog/login*/2018/06/26/messages |
awk -F"[/=,]" '
 {out[$2"\t"$4"-"$5]++}
 END {for(i in out) print out[i]"\t"i}
'
The string-addressed array ("out") keeps the strings unique, and its value is used for counting.
 
2 members found this post helpful.
Old 06-27-2018, 02:55 PM   #6
oliveoyl
Member
 
Registered: Sep 2016
Posts: 42

Original Poster
Rep: Reputation: Disabled
That works. Thank you.

Some explanation on the field separator -F"[/=,]" would be great. Teach me to fish.

Quote:
Originally Posted by MadeInGermany View Post
Thefollowing is based on the previous posts (untested)
Code:
awk '/ModuleUsageTracking/ {print $6}' /var/log/syslog/login*/2018/06/26/messages |
awk -F"[/=,]" '
 {out[$2"\t"$4"-"$5]++}
 END {for(i in out) print out[i]"\t"i}
'
The string-addressed array ("out") keeps the strings unique, and its value is used for counting.
 
Old 06-27-2018, 04:16 PM   #7
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,794

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
The [ ] is a character set.
[/=,] is a character that is either / or = or ,

A character set belongs to the regular expression; in fact the field separator is a regular expression.
 
Old 06-27-2018, 06:44 PM   #8
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,128

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Nice @MadeInGermany - there always exists a better way of doing things. Much neater than mine.

Some thoughts for the OP:
- character classes are generic, not just awk. "man grep" has a reasonable intro.
- grab the awk doco here. This is full user guide - there are a bunch of tutorials online, but this my "go to" for awk.
- associative arrays can take a while to get use to, but are amazingly effective.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Analyze Song Lyrics with a Shell Script, Part II LXer Syndicated Linux News 0 01-09-2018 09:27 PM
My First PHP script - MemoryReporter - Analyze Process with single script nijinashok00 Linux - Server 3 09-25-2013 02:50 AM
Analyze squid log files for analyze pattern harshaabba Linux - Software 1 10-13-2011 09:21 PM
Can someone analyze this command? christopher_c Linux - Newbie 3 05-02-2008 05:27 PM
Shell script to analyze VoIP packets shane200_ Linux - Networking 1 04-14-2006 02:28 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 11:59 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration