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.
空白で列に区切られたデータを含むテキスト ファイルがあるとします。ファイル名と数値 N を入力として取り、その列のみを出力するシェルスクリプトを書きたいと思います。awk を使用すると、次のことができます。
awk < /tmp/in '{print $2}' > /tmp/out
このコードは、2 番目の列を出力します。
しかし、任意の列をargvで渡すことができるように、シェルスクリプトでそれをラップするにはどうすればよいでしょうか?
awk -vx=2 '{print $x}'
またはシェルスクリプトで:
#!/ビン/sh 数=$1 awk < /tmp/in -vx=$num '{print $x}' > /tmp/out
awk '{print $'$myvar'}' < /tmp/in > /tmp/out
$myvar は変数列 (整数) です。スクリプト インジェクションに注意してください。