gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 21:44:04 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com