Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
假设我有一个文本文件,其中的数据由空格分隔成列。我想编写一个 shell 脚本,它将文件名和数字 N 作为输入,并仅打印出该列。使用 awk 我可以执行以下操作:
awk < /tmp/in '{print $2}' > /tmp/out
此代码打印出第二列。
但是如何将其包装在 shell 脚本中,以便可以在 argv 中传递任意列?
awk -vx=2 '{打印 $x}'
或在 shell 脚本中:
#!/bin/sh 数量=$1 awk < /tmp/in -vx=$num '{print $x}' > /tmp/out
awk '{print $'$myvar'}' < /tmp/in > /tmp/out
其中 $myvar 是您的变量列(整数)。注意脚本注入!