0

I have a txt file with 3 rows, now I want to multiply a variable value with the 2nd column. Given below is my requirement.

cat sample.txt
    ABC,123,qwe123
    ASD,456,asd123
    VBN,6532,wqe123

I have a variable, colum=100. I want to multiply this variable with 2nd column using awk. The code I tried is:

cat sample.txt | awk -F , '{print $1","$2*$($colum)","$3}'

My expected output is:
        ABC,12300,qwe123
        ASD,45600,asd123
        VBN,653200,wqe123

Please note that I want to perform this awk using the variable only, I dont want to directly multiply column 2 with 100.

Programmer
  • 329
  • 2
  • 6
  • 25
  • Use the builtin `ENVIRON` array to access environment variables: `colum=100 awk -F , '{print $1","$2*ENVIRON["colum"]","$3}' sample.txt` – Phylogenesis Sep 29 '15 at 08:28

1 Answers1

2

You can pass a variable to awk using -v option

cat sample.txt | awk -v colum="100" -F, '{print $1 "," $2 * colum, $3}'
ramana_k
  • 1,933
  • 2
  • 10
  • 14