Thursday, August 27, 2009

Obtaining a specific field from a comma separated file (using awk)

If you have a comma separated file e.g.
Name,DateOfBirth,PlaceOfBirth
Jeff,17-09-1982,London
Maggie,08-06-1976,New York
George,14-02-1991,Johannesburg


and you need to extract only the list date of birth from this file you can use the following awk command:
awk 'BEGIN{FS=","}{print $2}' filename > date_of_birth

where FS indicates the file separator (so if your file is delimited with a different character e.g. ":" you would have FS=":" within the awk command) and "$2" indicates the 2nd column.