U have a file
File 1
0.3 0.2 0.4 0.3 0.8 0.11 0.22
0.4 0.23 0.45 0.56 0.78 0.98
0.11 0.32 0.2 0.4 0.45 0.54
0.2 0.33 0.44 0.21 0.22 0.98
0.8 0.2 0.34 0.54 0.98 0.12
0.1 0.22 0.32 0.34 0.89 0.22
File 2
rows columns
3 1
2 3
4 2
5 5
so from file 2 into file 1
output will be
0.11
0.45
0.2
0.89
I have used using awk
awk -v num=3 '{if(NR==num) print($0);}' File1 | awk '{print $1}'
awk -v num=2 '{if(NR==num) print($0);}' File1 | awk '{print $3}
Now problem is it is long manual process as it is having big file
So any done using perl or shell script..????
awk '{ printf "NR == %d { print $%d }\n", $1, $2 }' File2 |
awk -f - File1
Nice one
And another one, assuming typos in your example output:
(use gawk, nawk or /usr/xpg4/bin/awk on Solaris)
awk 'NR == FNR { _[$1] = $2; next }
FNR in _ { print $_[FNR] }' File2 File1
Thanks for the reply
But after running the program the output is
0.45
0.11
0.33
0.98
But is shud be
0.11
0.45
0.33
0.98
Would you mind to explain the logic behind?
Ygor
April 23, 2009, 1:50am
6
Try...
awk 'FNR==NR{for(i=1;i<=NF;i++)a[NR,i]=$i;next}{print a[$1,$2]}' file1 file2
...gives...0.11
0.45
0.33
0.98
Dear Radulov,
actually overall output was right in using this awk command but in random order as u can see in output
first one came as second order and second came as first as a large file is there it is confusing where it is ? But command wise is right
Another latest reply is giving right answer in orderwise
Thanks
nawk '{
if(NR==FNR){
n=split($0,tmp," ")
for(i=1;i<=n;i++)
arr[NR" "i]=tmp
}
else{
print arr[$0]
}
}' file1 file2