在awk中制表符分隔的值

如何从TAB分隔的string中select第一列?

# echo "LOAD_SETTLED LOAD_INIT 2011-01-13 03:50:01" | awk -F'\t' '{print $1}' 

以上将返回整个行,而不是像预期的那样“LOAD_SETTLED”。

更新:

我需要更改选项卡分隔值中的第三列。 以下不起作用。

 echo $line | awk 'BEGIN { -v var="$mycol_new" FS = "[ \t]+" } ; { print $1 $2 var $4 $5 $6 $7 $8 $9 }' >> /pdump/temp.txt 

但是,如果分隔符是逗号而不是制表符,则按预期工作。

 echo $line | awk -v var="$mycol_new" -F'\t' '{print $1 "," $2 "," var "," $4 "," $5 "," $6 "," $7 "," $8 "," $9 "}' >> /pdump/temp.txt 

您需要将OFSvariables(输出字段分隔符)设置为一个选项卡:

 echo "$line" | awk -v var="$mycol_new" -F $'\t' 'BEGIN {OFS = FS} {$3 = var; print}' 

(确保在echo语句中引用$linevariables)

确保他们真的是标签! 在bash中,你可以使用Cv TAB插入一个选项卡

 $ echo "LOAD_SETTLED LOAD_INIT 2011-01-13 03:50:01" | awk -F$'\t' '{print $1}' LOAD_SETTLED 

您可以设置字段分隔符:

 ... | awk 'BEGIN {FS="\t"}; {print $1}' 

优秀阅读:

https://docs.freebsd.org/info/gawk/gawk.info.Field_Separators.html

 echo "LOAD_SETTLED LOAD_INIT 2011-01-13 03:50:01" | awk -v var="test" 'BEGIN { FS = "[ \t]+" } ; { print $1 "\t" var "\t" $3 }' 

如果这不起作用?

 echo "LOAD_SETTLED LOAD_INIT 2011-01-13 03:50:01" | awk '{print $1}'