Ubuntu – How to use double substitution in awk

awkbashcommand linescripts

To print the fields at 29th column in two files I used

paste <(awk -F, '{print $29}' PreRefFile.csv) <(awk -F, '{print $29}' Txlog.csv)

This worked fine.
To print all the fields starting from 29 to 189. I wrote a script as follows

 y=29
 while [ $y -le 189 ]
 do
   x="\$$y"
   paste <(awk -F, '{print "'"$x"'"}' PreRefFile.csv) <(awk -F, '{print "'"$x"'"}' Txlog.csv)
   y=`expr $y + 1`
 done

Here the value of x is replaced by "$" followed by number (First round of substitution) and it is printing "$" follwed by that number instead of printing the field at that location. How to get that field in this way.
I cannot write the same line for so many times. Suggest a method to proceed.

Also suggest another tool to do this, other than awk

Best Answer

All what you need is the power of awk and a for Statement:

paste <(awk -F, '{ for (i=29;i<=188; i++) print $i }' PreRefFile.csv) <(awk -F, '{ for (i= 29;i<= 188;i++) print $i }' Txlog.csv)

My test case:

paste <(awk -F, '{ for (i=2;i<=3;i++) print $i }' foo1) <(awk -F, '{ for (i=2;i<=3;i++) print $i }' foo2)

File foo1:

1,2,3,4,5,6
7,8,9,10,11,12

File foo2:

a,b,c,d,e,f,g
A,B,C,D,E,F,G

Output:

2   b
3   c
8   B
9   C
Related Question