Dale Pin Dale Pin - 1 year ago 90
Linux Question

awk command: how to tell it to distinguish between space and tab delimiter

I used this command,

awk '{print $1}' "$line"
, to get a column of data from a file that looks like this:(underscores represent tab as a delimiter, example is a three-column data)

Joe Jonas_____8_____45
Cersei Lann_____4_____23
Bo Tox_____6_____28
Mis Ter Yo_____7_____89

When I run the command, say to get the first column, the output, however, showed it like this:


How can I make the command to ignore the spaces within the data of the first column?

Answer Source

Change the field separator (-F) to tab(s):

$ awk -F$'\t*' '{gsub(/ /,"_",$1); print $1,$2}' test2.in
Joe_Jonas 8
Cersei_Lann 4
Bo_Tox 6
Mis_Ter_Yo 7

EDIT: Use gsub on first column to replace space with underscore.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download