user2488578 user2488578 - 1 year ago 87
Bash Question

awk : loop through tab delimited file

Input csv file has the below content, which is tab delimited.

3915 LOW Thia Khiang LIU

I need to find 4th item which happens to be "LIU" in the above case.

But, below script is printing "Khiang". It is considering space as tab.

awk '{print "UID="$1"\tLN="$2"\tAlias="$4}' myInputFile.csv

Answer Source

Specify tab as the field separator explicitly:

$ awk -F'\t' '{print "UID="$1"\tLN="$2"\tAlias="$4}' myInputFile.csv
UID=3915    LN=LOW  Alias=LIU

By default (and when specifying a literal space as the field separator), awk considers any line-internal run of spaces and/or tabs a (single) field separator (and additionally trims leading and trailing whitespace).