PDA

View Full Version : awk 'print' in a bash script


darth_vector
September 18th, 2006, 11:49 PM
i am trying to write a bash script that requires the kind of functionality shown below:#!/bin/bash
FILE=$1
COL_TO_PRINT=$2
cat $FILE | awk '{print $COL_TO_PRINT}'and for the life of me I can't work out how to do it. of course doing an awk '{print $2}'wont work because awk interprets this as the second column of the file, not the second argument of the script.

any help would be appreciated...

cwaldbieser
September 19th, 2006, 01:10 AM
i am trying to write a bash script that requires the kind of functionality shown below:#!/bin/bash
FILE=$1
COL_TO_PRINT=$2
cat $FILE | awk '{print $COL_TO_PRINT}'and for the life of me I can't work out how to do it. of course doing an awk '{print $2}'wont work because awk interprets this as the second column of the file, not the second argument of the script.

any help would be appreciated...


$ awk '{print ENVIRON["COL_TO_PRINT"];}' $FILE

You could also use the "awk var=value 'program goes here'" syntax.

darth_vector
September 19th, 2006, 01:33 AM
cheers, i will give her a go this arvo...

darth_vector
September 19th, 2006, 09:48 AM
after doing anexport COL_TO_PRINTit worked like a dream.

thanks mate :)