如何根据第1列bash脚本sed awk的值从第2列中选择最大数

如何根据第1列bash脚本sed awk的值从第2列中选择最大数,bash,sed,awk,Bash,Sed,Awk,我有这样一个文件: 1 12:00 1 12:34 1 01:01 1 08:06 2 09:56 2 06:52 ... 我想从第一列的每个值中选择第二列的最大值 新文件: 1 12:34 2 09:56 ... 我该怎么做??提前谢谢 awk ' { if ($2>values[$1]) values[$1]=$2; } END { for (i in values) { print i,values[i] } } ' file

我有这样一个文件:

1 12:00

1 12:34

1 01:01

1 08:06

2 09:56

2 06:52
...
我想从第一列的每个值中选择第二列的最大值

新文件:

1 12:34

2 09:56 
...
我该怎么做??提前谢谢

awk '
  { if ($2>values[$1]) values[$1]=$2; }
  END {
    for (i in values) {
      print i,values[i]
    }
  }
' file

这可能适合您:

sort -k1,1n -k2,2r file | sort -uk1,1
仅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
# 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