无论我使用 colNb 还是 $colNb,都不起作用
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
有了 $colNb,我明白了
awk: illegal field $(), name "colNb"
input record number 1, file1
source line number 1
对于 colNb,我只得到空字段而不是 file1 中的字段
最佳答案
在 awk 中使用 -v
选项将 shell 变量传递给 awk:
awk -v colNb="$colNb" '{for(i=3 ; i<colNb; i++) {printf("%s\t", $i)} print ""}' file1 | less
关于bash - awk 在 bash 中使用的变量集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19845374/