bash - awk 在 bash 中使用的变量集

标签 bash variables awk

无论我使用 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/

相关文章:

linux - 使用 awk/sed 重新格式化长文件

linux shell存放路径

Scala:处理元组返回值是否需要临时变量?

python - tensorflow 变量名称中允许使用哪些字符?

MySQL 变量值在 WHERE 子句中未更改

arrays - 我可以使用 -v 将数组传递给 awk 吗?

Linux Ubuntu : Script works in terminal, 但不是 .sh

php - 错误替换 Bash : how to write a heredoc to a dynamically created file (variable)?

bash脚本: Delete characters in string up to and including a phrase?

awk 使用 if 语句在一个句子中获取多行的结果