awk script doubt in extracting and comparing uid


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk script doubt in extracting and comparing uid
# 1  
Old 07-03-2010
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.

Code:
$ cat f1
/home/joe       pc001(rw,all_squash,anonuid=150,anongid=100)

Code:
awk '{sub(/.*,anonuid=/,"");sub(/,.*$/,"")}1' f1

gives me the expected result 150

However, the below command is not working as expected
Code:
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

Last edited by royalibrahim; 07-04-2010 at 06:17 AM..
# 2  
Old 07-03-2010
Hi

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

Code:
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:

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

Guru.

Last edited by guruprasadpr; 07-03-2010 at 10:54 AM.. Reason: one more option
# 3  
Old 07-04-2010
Thanks a lot Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extracting and comparing values

I was trying to extract value of g1 and p1 only inside the tags where t1 is "Reading C (bytes)" and comparing them to make sure p1 is always less than g1. Here is the Json file I'm using - File:- { "g1" : 1482568, "n1" : "v_4", "p1" : 0, "s1" : "RC", "t1" : "LM", } { "g1" :... (3 Replies)
Discussion started by: Mannu2525
3 Replies

2. Shell Programming and Scripting

Comparing two csv file fields using awk script

Hi All, I want to remove the rows from File1.csv by comparing the columns/fields in the File2.csv. I only need the records whose first column is same and the second column is different for the same record in both files.Here is an example on what I need. File1.csv: RAJAK|ACTIVE|1... (2 Replies)
Discussion started by: rajak.net
2 Replies

3. Shell Programming and Scripting

Comparing the two files using awk script

Hi all, Can you please help me to find out that where is the problem in my script or either my way of writing the shell command on the prompt is not right? Actually, I want to compare the second column "$1" of the file "t1" with all the columns of second file "t2", if there is a match then the... (2 Replies)
Discussion started by: coder83
2 Replies

4. Shell Programming and Scripting

Extracting a portion of the string and comparing

I have 2 text files say file1.txt and file2.txt . Some of the sample records for file1.txt were shown below: XXXXX12345XXXXXXX12 3456789YYYYY XXXXXXXXXX12345XX123457485YYYYY XX12345XXXXXXXXXX123454658YYYYY for file2.txt, some of the sample records were shown below: ... (5 Replies)
Discussion started by: bobby1015
5 Replies

5. Shell Programming and Scripting

awk script issue : comparing two files with a pattern

File 1 ################################################################# pma.zcal.iop_pma_zcal_cntl (2710.080 115.200) pma.lanea23.rx0.cntl (696.960 844.800) pma.lanea67.rx0.cntl (1733.760 844.800) pma.zcal.iop_pma_zcal_cust (2280.960 115.200)... (1 Reply)
Discussion started by: jaita
1 Replies

6. Shell Programming and Scripting

sed doubt in extracting

Hi, Can anyone help me in understanding how the below code works? echo "texxt" | sed 's///' gives output exxt, ideally it should give xxt. as this should remove the chars which is not x. echo 'x_a_b_a_c_a_d' | sed 's/.*\(a\)/\1/' gives output as a_d, which should be 'a' as it's the only... (2 Replies)
Discussion started by: royalibrahim
2 Replies

7. Shell Programming and Scripting

doubt while extracting file

Hi friends i am very new to unix.i have one doubt (2 Replies)
Discussion started by: Gopal_Engg
2 Replies

8. Shell Programming and Scripting

Awk script / comparing two files

Goal: To combine the data from two files into one file. File1 = 11 fields, the first field is the unique key File2 = 2 fields, the first field is the unique key What I want to do is match File2:column1 with File1:column1 and if it matches, to add the data from File2:column2 to the matching... (2 Replies)
Discussion started by: jmcgranahan
2 Replies

9. UNIX for Advanced & Expert Users

i have a doubt with the awk script??

Hi Guys, I am new to this forum, i have doubt with a awk script. I want to generate a list where i can busiest hour from a list of a data. 05/19/2008 14:27:19 - avg: 0 max: 0 min: 0 samples: 0 total: 0 05/19/2008 14:28:19 - avg: 0 max: 0 min: 0 samples: 0 total: 0 05/19/2008 14:29:19 -... (2 Replies)
Discussion started by: asirohi
2 Replies

10. UNIX for Dummies Questions & Answers

Doubt over Uid and User name.

Hi, I know the uid and I wan to know the user name the uid belongs to. How can I get it. Suppose My user name is ssnayak and coresponding uid is 1110 Similarly I know one uid 1212 and how can I come to know the user name for this uid. Thanks & Regards, Siba (3 Replies)
Discussion started by: siba.s.nayak
3 Replies
Login or Register to Ask a Question