awk script doubt in extracting and comparing uid

Hi,

I need to get the value of 'uid' from a line in file f1. For example, in the line below, I need to get the value '150', from the 'anonuid=150' key, and I need to verify that this is a valid uid by checking it against /etc/passwd (i.e) I want to compare this uid, 150 (or more if there are any more entries in
f1) to the 3rd field in /etc/passwd file. If there is a match with the entries in /etc/passwd file then this uid should be displayed.

$ cat f1
/home/joe       pc001(rw,all_squash,anonuid=150,anongid=100)
awk '{sub(/.*,anonuid=/,"");sub(/,.*$/,"")}1' f1

gives me the expected result 150

However, the below command is not working as expected

awk -F: 'NR==FNR {sub(/.*,anonuid=/,"");sub(/,.*$/,""); a[$0]; next} $3 in a {delete a[$3]} END {for (id in a) print "uid " id " found"}' f1 /etc/passwd

I have one entry having uid=150 in /etc/passwd.

Please suggest me what correction I should make to the above command to make it work

Hi

Looks to me, just a not is missing. Try this:

awk -F: 'NR==FNR {sub(/.*,anonuid=/,"");sub(/,.*$/,""); a[$0]; next} !($3 in a) {delete a[$3]} END {for (id in a) print "uid " id " found"}' f1 /etc/passwd

or, you can also try this:

awk -F: 'NR==FNR {sub(/.*,anonuid=/,"");sub(/,.*$/,""); a[$0]; next} $3 in a{print "uid " $3 " found"}' f1 /etc/passwd

Guru.

Thanks a lot :slight_smile: