I have few files as:
ifile1.txt ifile2.txt ifile3.txt
3 5 2 2 1 2 1 3 4 3 4 1
1 4 2 1 1 3 0 2 5 3 1 5
4 6 5 2 2 5 5 1 3 4 3 1
5 5 7 1 0 0 1 1 4 3 4 0
2 3 2 4 3 2 4 1 3 1 2 1
I need to copy 4th row from each file and paste as columns in ofile.txt as:
ofile.txt
5 0 4
5 0 3
7 1 4
1 1 0
I was able to do it in the following way, but looking for a direct/short method.
I first converted the rows of each file into columns using awk
awk '{
for (i=1; i<=NF; i++) {
a[NR,i] = $i
}
}
NF>p { p = NF }
END {
for(j=1; j<=p; j++) {
str=a[1,j]
for(i=2; i<=NR; i++){
str=str" "a[i,j];
}
print str
}
}' ifile1.txt > ofile1.txt
Then I used used paste
command as
paste ofile* > ofile.txt
Finally used awk
again to print the required columns.