1

I am using this awk command to extract three rows from a text file.

awk 'BEGIN {FS="\t";OFS=","}; {print $1,$3,$10}' $FILENAME > $OUTPUT

I wish to specify the column numbers as a variable separately so it will be easier to modify in the future like this:

COLUMNS=$1,$3,$10
awk 'BEGIN {FS="\t";OFS=","}; {print $COLUMNS}' $FILENAME > $OUTPUT

However it pulls all columns into the output, not only the 3 I specified. How do I do this properly?

1
  • Your trapped by the shell quoting necessities/rules/limits. Try echo $COLUMNS and start getting enlightened. ;-) Commented Nov 8, 2015 at 16:13

1 Answer 1

1

like this ?

$ more file
a,b,c,d,e
1,2,3,4,5

$ a='$1,$2,$NF'

$ awk -F, "{print $a}" file
a b e
1 2 5
Sign up to request clarification or add additional context in comments.

1 Comment

@Am_I_Helpful: The proposal works. So why doesn't this answer the OP's question please?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.