akbar
June 12, 2008, 6:27pm
1
I'm auditing UID consistency across our hosts, and have created the following datafile, consisting of four fields. I would like to get a count of the combination of the last two fields. ie: I would like to find out how many instances there are of "root 0" and how many of "uucp 5", for every line in the file. I know basic perl and basic awk, but can't get my head around how to do this.
Can anyone offer advice?
thank you in advance
akbar
UID: crfw root 0
UID: crfw daemon 1
UID: crfw bin 2
UID: crfw sys 3
UID: crfw adm 4
UID: crfw lp 71
UID: crfw uucp 5
UID: crfw nuucp 9
UID: crfw smmsp 25
UID: crfw listen 37
UID: crfw gdm 50
UID: crfw webservd 80
UID: crfw nobody 60001
UID: crfw noaccess 60002
UID: creb root 0
UID: creb daemon 1
UID: creb bin 2
UID: creb sys 3
UID: creb adm 4
UID: creb lp 71
UID: creb uucp 5
UID: creb nuucp 9
UID: creb smmsp 25
UID: creb listen 37
UID: creb gdm 50
UID: creb webservd 80
UID: creb nobody 60001
UID: creb noaccess 60003
awk '/(root|[^n]uucp)/ {
total[$(NF-1)]++
}END {
for( i in total) {
print i,total
}
}' file
drl
June 13, 2008, 2:03pm
3
Hi.
If you want to enumerate all the like items:
#!/bin/bash -
# @(#) s1 Demonstrate counting occurrences.
echo
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version =o $(_eat $0 $1) cut sort uniq
echo
FILE=${1-data1}
cut -d" " -f3- $FILE |
sort |
uniq -c
exit 0
Producing:
$ ./s1
(Versions displayed with local utility "version")
SunOS 5.10
GNU bash 3.00.16
cut - no version provided for /usr/bin/cut.
sort - no version provided for /usr/xpg4/bin/sort.
uniq - no version provided for /usr/bin/uniq.
2 adm 4
2 bin 2
2 daemon 1
2 gdm 50
2 listen 37
2 lp 71
1 noaccess 60002
1 noaccess 60003
2 nobody 60001
2 nuucp 9
2 root 0
2 smmsp 25
2 sys 3
2 uucp 5
2 webservd 80
See man pages for details ... cheers, drl
joeyg
June 13, 2008, 2:13pm
4
> cat uid_data | cut -d" " -f3-4 | sort | uniq -c
2 adm 4
2 bin 2
2 daemon 1
2 gdm 50
2 listen 37
2 lp 71
1 noaccess 60002
1 noaccess 60003
2 nobody 60001
2 nuucp 9
2 root 0
2 smmsp 25
2 sys 3
2 uucp 5
2 webservd 80