I am using Ubuntu and I have an input file like this
ifile.dat
1 10 15
3 34 20
1 4 22
3 32 33
5 3 46
2 2 98
4 20 100
3 13 23
4 50 65
1 40 76
2 20 22
How do I achieve this?
ofile.dat
1 40 76
2 20 98
3 34 33
4 50 100
5 3 46
I mean the max of each column by comparing first column. Thanks.
Here is what I have tried(on a sample file with 13columns). But the highest value is not coming up this way.
cat input.txt | sort -k1,1 -k2,2nr -k3,3nr -k4,4nr -k5,5nr -k6,6nr -k7,7nr -k8,8nr -k9,9nr -k10,10nr -nrk11,11 -nrk12,12 -nrk13,13 | sort -k1,1 -u
It didn't work. So a helpful guy tried to help me with this below. But no matter on mac or ubuntu with gawk, I couldn't run it and see the errors below
awk 'BEGIN{PROCINFO["sorted_in"] = "@val_num_asc"} {for(i=2;i<=NF;++i) if (a[$1][i]<$i){a[$1][i]=$i}} END{n=asorti(a, asorted); for(col1 in asorted){print col1, a[col1][2], a[col1][3]}}' input.txt
Error is
awk: syntax error at source line 1
context is
BEGIN{PROCINFO["sorted_in"] = "@val_num_asc"} {for(i=2;i<=NF;++i) if >>> (a[$1][ <<<
awk: illegal statement at source line 1
awk: illegal statement at source line 1
I did try removing the BEGIN statement and by playing with the for loop, but couldn't find luck. Thanks.
P.S.: I got this answer from stackoverflow. So I am posting it here because this is a unix/linux special forum.
mawk.