简体   繁体   English

调用AWK和Shell脚本

[英]Calling AWK and Shell Scripting

I'm having a problem with a Shell Script. 我的Shell脚本有问题。

In a nutshell I want to print the "$2" column of the file "$1" in my script "showColumn" 简而言之,我想在脚本“ showColumn”中打印文件“ $ 1”的“ $ 2”列

So, what I've tried: 所以,我试过了:

#!/bin/bash
awk '{print $($2)}' $1
exit 0

Ok, I know that $($2) is totally incorrect, but you can see what I'm trying to do. 好的,我知道$($ 2)完全不正确,但是您可以看到我正在尝试执行的操作。

So what I want when I write "./showColumn file.txt 2" is printing the second column of file.txt but I don't know how to solve that. 因此,当我编写“ ./showColumn file.txt 2”时,我想要打印的是file.txt的第二列,但我不知道该如何解决。

Try: 尝试:

#!/bin/bash
awk -v column="$2" '{print $column}' "$1"
exit 0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM