Wednesday, May 8, 2024
4
rated 0 times [  4] [ 0]  / answers: 1 / hits: 780  / 1 Year ago, fri, february 24, 2023, 12:04:05

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


More From » command-line

 Answers
1

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

[#20432] Friday, February 24, 2023, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
lassrake

Total Points: 400
Total Questions: 103
Total Answers: 98

Location: Netherlands
Member since Mon, Jun 22, 2020
4 Years ago
;