How do I select the first column from the TAB separated string?
# echo "LOAD_SETTLED LOAD_INIT 2011-01-13 03:50:01" | awk -F'\t' '{print $1}'
The above will return the entire line and not just "LOAD_SETTLED" as expected.
Update:
I need to change the third column in the tab separated values. The following does not work.
echo $line | awk 'BEGIN { -v var="$mycol_new" FS = "[ \t]+" } ; { print $1 $2 var $4 $5 $6 $7 $8 $9 }' >> /pdump/temp.txt
This however works as expected if the separator is comma instead of tab.
echo $line | awk -v var="$mycol_new" -F'\t' '{print $1 "," $2 "," var "," $4 "," $5 "," $6 "," $7 "," $8 "," $9 "}' >> /pdump/temp.txt
awk 'BEGIN {FS="\t"}; {print $1,FS,$2,FS,$3}' myFile.txt
to print tab-delimited values of the first three columns. - Wokawk 'BEGIN {OFS="\t"}; {print $1,$2,$3}'
- Josiah Yoder-v
for setting variables. It's ugly to useBEGIN {FS="\t"}
inside an inline program, and any open source contribution you try to make like that is likely to be objected to. Only do that if you are writing a program file. Also, it is discouraged to use-F
instead of-v FS=
because the latter makes clear that onlyFS
is being set and notOFS
. Confusion about that last point is what caused this post in the first place. That's why "good style" is important. - Bruno BronoskyOFS
variable. - Bruno Bronosky