简体   繁体   中英

Calling AWK and Shell Scripting

I'm having a problem with a Shell Script.

In a nutshell I want to print the "$2" column of the file "$1" in my script "showColumn"

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.

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.

Try:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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