Linux 如何使用awk打印文件中的第n列

Linux 如何使用awk打印文件中的第n列,linux,bash,awk,Linux,Bash,Awk,如何使用awk打印文件中的第n列?要打印文件的第二列,我尝试: #!/bin/bash awk '{print $2}' file 但是如果n是一个变量,如何使用awk打印文件的第n列 #!/bin/bash n=2 尝试一下,注意-v选项: #!/bin/bash n=3 awk -v x=${n} '{print $x}' file 从手册页: The option -v followed by var=value is an assignment to be done

如何使用
awk
打印文件中的第n列?要打印文件的第二列,我尝试:

#!/bin/bash   
awk '{print $2}' file
但是如果
n
是一个变量,如何使用
awk
打印文件的第n列

#!/bin/bash   
n=2

尝试一下,注意
-v
选项:

#!/bin/bash

n=3

awk -v x=${n} '{print $x}' file
从手册页:

The option -v followed by var=value is an assignment to be done before 
prog is executed; any number of -v options may be present.

有关更多示例,您可以查看hi,
awk'{n=2;print$n}
file这是一个副本,您可以在
awk
中看到许多关于使用shell变量的类似问题。标题可能建议使用awk脚本,但都是一样的,在脚本(或)命令中使用shell变量
The option -v followed by var=value is an assignment to be done before 
prog is executed; any number of -v options may be present.