How to specify *one* tab as field separator in AWK? -
the default white-space field separators, such tab when using fs = "\t"
, in awk
either 1 or many. therefore, if want read in tab separated file null values in columns (other last), skips on them. example:
1 "\t" 2 "\t" "" "\t" 4 "\t" 5
$3
refer 4
, not null ""
though there 2 tabs.
what should can specify field separator 1 tab only, $4
refer 4
, not 5
?
echo '1 "\t" 2 "\t" "" "\t" 4 "\t" 5' | awk -f"\t" '{print "$3="$3 , "$4="$4}'
output
$3=" "" " $4=" 4 "
so can remove dbl-quotes in original string, , get
echo '1\t2\t\t4\t5' | awk -f"\t" '{print "$3="$3 , "$4="$4}'
output2
$3= $4=4
you're right, default fs white space, caveat space , tab char next each other, qualify 1 fs instance. use "\t" fs, can above cmd-line argument, or can include explict reset on fs, done in begin
block, like
echo '1 "\t" 2 "\t" "" "\t" 4 "\t" 5' | awk 'begin{fs="\t"}{print "$3="$3 , "$4="$4}'
ihth
Comments
Post a Comment