Thursday, February 14, 2008

grep and put the SL no - AWK Newbie


The purpose is very simple, I had to find all people names(1st field) from location(3rd field) "Ind", with a SL No in beginning. i.e. if the input files are like this:

$ cat file1.txt
Hanr:3232:Ind:/tmp/da02.out
Ajith:2211:Ind:/opt/ds.out
Rein:2221:Jpn:/tmp/ls.out
Leela:1198:Ind:/

$ cat file2.txt
Olea:5454:Swd:/opt/ert.tmp
Lola:2129:Chn:/home/Lola
Nina:8784:Ind:/opt/ls.out
Rahim:6551:Ind:/tmp/as.out

The output required is :
1 Hanr
2 Ajith
3 Leela
4 Nina
5 Rahim

Here is the solution:
$ awk -F ":" '$3=="Ind" {c++;print c,$1}' file1.txt file2.txt

No comments:

© Jadu Saikia www.UNIXCL.com