Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

AWK help 1

Status
Not open for further replies.

dbadmin

Programmer
Jan 3, 2003
147
US
I have a file which has entries like this

dat1,dat2,dat3
datm,datn,dato
#dat1,dat3,data
dat2,datp,datq
daty,dat1,datb

The requirement which I have is to find the 2nd field when the first field is a particular value.

Assume the value I am checking is dat1 and the code should return dat2. If I use grep,the first, 3rd and 5th record will be returned. I can use awk to get the first field
and then remove the record with # and then get the second record, but I am unable to write the code for it. Could any one help me please.

Thanks,
dbadmin
 
If I use grep,the first, 3rd and 5th record will be returned
Even this ?
grep '^dat1,' /path/to/input

As you asked for awk:
awk -F, '$1=="dat1"{print $2}' /path/to/input

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Hi,

Thanks for the quick response. The ^ will help me but the problem will be, if I have an entry like dat12 that also will be returned.

dbadmin
 
Reread carefully my post !
grep '[!]^[/!]dat1[!],[/!]' /path/to/input

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
It works like a charm. Thanks a lot

dbadmin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top