I have a file like this:
1 12:00 1 12:34 1 01:01 1 08:06 2 09:56 2 06:52 ...
and I want to select from each value of the first column the largest value of the second.
New file:
1 12:34 2 09:56 ...
How can i do this? Thanks in advance!
awk ' { if ($2>values[$1]) values[$1]=$2; } END { for (i in values) { print i,values[i] } } ' file
This might work for you:
sort -k1,1n -k2,2r file | sort -uk1,1
perl -nale '$F[1]=~s/://;$h{$F[0]}=$F[1]if($F[1]>$h{$F[0]}); END{for(sort keys(%h)){($x=$h{$_})=~s/^(..)(..)$/\1:\2/;print"$_ $x"}}' file
Look it up
Bash
# build arrays with 1.column value in its name # use all digits in the row as index, the row as value while read a b ; do eval "array$a[$a${b//:/}]=\"$a $b\"" done < "$infile" # select the last element of each array for name in ${!array*}; do last='${'${name}'[-1]}' eval "echo ${last}" done