1

whether I use colNb or $colNb, it doesn't work

export colNb=$(awk '{print NF}' file1 | sort -nu | tail -n 1)

awk '{for(i=3 ; i<colNb; i++) {printf("%s\t", $i)} print ""}' file1 | less

with $colNb, I get

awk: illegal field $(), name "colNb"
 input record number 1, file1
 source line number 1

and with colNb, I just get empty fields instead of the fields in file1

4

2 回答 2

3

在 awk 中使用-voption 将 shell 变量传递给 awk:

awk -v colNb="$colNb" '{for(i=3 ; i<colNb; i++) {printf("%s\t", $i)} print ""}' file1 | less
于 2013-11-07T19:57:31.493 回答
2

这样做

awk '{for(i=3 ; i<c; i++) {printf("%s\t", $i)} print ""}' c="$colNb" file1 | less

您需要使用 awk-v或在 awk 之后设置变量

于 2013-11-07T19:57:43.707 回答