gpt4 book ai didi

linux - 如何排除第一列和第二列具有相同值的行?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:44:53 24 4
gpt4 key购买 nike

我有一个文件,其中一些行具有相同的名称(第 1 列,例如 gph ans stp),但在第 2 列最大值中,并排除了 linux 中的其他行(shell,例如 awk grep perl)

例如:

col1 | col2 | col3   | col4
------+------+--------+-----------
ghp | 25 | data1 | otherdata1
ghp | 23 | data2 | otherdata2
ghp | 23 | data3 | otherdata3
jkl | 58 | data4 | otherdata4
stp | 42 | data5 | otherdata5
stp | 41 | data6 | otherdata6
stp | 41 | data7 | otherdata7

结果

 col1 | col2 | col3   | col4
------+------+--------+-----------
ghp | 25 | data1 | otherdata2
jkl | 58 | data4 | otherdata4
stp | 42 | data5 | otherdata7

在同一个文件中,我有一些具有相同名称的行(col 1,例如 ghi ans stu),在 col 2 中有相同的值,我想在 linux 中排除这些行(shell,例如 awk grep perl)

例如

col1 | col2 | col3   | col4
------+------+--------+-----------
ghi | 23 | data1 | otherdata1
ghi | 23 | data2 | otherdata2
ghi | 23 | data3 | otherdata3
jkl | 58 | data4 | otherdata4
stu | 41 | data5 | otherdata5
stu | 41 | data6 | otherdata6
stu | 41 | data7 | otherdata7

结果:

 col1 | col2 | col3   | col4
------+------+--------+-----------
jkl | 58 | data4 | otherdata4

谢谢

最佳答案

您可以使用这个 awk 命令:

awk 'NR<=2{print; next} $2>max[$1]{max[$1]=$2; a[$1]=$0} END{for (i in a) print a[i]}' file

col1 | col2 | col3 | col4
------+------+--------+-----------
ghp | 25 | data1 | otherdata1
jkl | 58 | data4 | otherdata4
stp | 42 | data5 | otherdata5

关于linux - 如何排除第一列和第二列具有相同值的行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38316075/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com