gpt4 book ai didi

linux - 如何在使用 linux shell 时过滤多个文件并消除重复条目以选择单个条目

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:25:48 27 4
gpt4 key购买 nike

我有一个包含多个文件的文件夹。这些文件由相同的列组成。假设file1和file2的内容如下。(这里可以是两个以上的文件)

 
$cat file1.txt
9999999999|1200
8888888888|1400
7777777777|1255
6666666666|1788
7777777777|1289
9999999999|1300


$cat file2.txt
9999999999|2500
8888888888|2450
6666666666|2788
9999999999|3000
2222222222|3001

In my file 1st column is mobile number and 2nd is count. Same mobile can be there in multiple files. Now I want to get the records into a file with unique mobile numbers which has the highest count.The output should be as follows:


$cat output.txt
7777777777|1289
8888888888|2450
6666666666|2788
9999999999|3000
2222222222|3001

如有任何帮助,我们将不胜感激。

最佳答案

这可能不是很有效,但它完成了工作:将其放入 phones.sh 并运行 sh phones.sh

#!/bin/bash
files="
file1.txt
file2.txt
"
phones=$(cat $files | cut -d'|' -f1 | sort -u)
for phone in $phones; do grep -h $phone $files | sort -t'|' -k 2 -nr | head -n1; done | sort -t'|' -k 2

它所做的基本上是,提取文件中的所有电话号码,遍历它们并在所有文件中对它们进行 grep,选择计数最高的那个。然后我还按计数对最终结果进行了排序,这就是您的预期结果所暗示的。 排序-t'|' -k 2 -nr 表示给定分隔符 | 的第二列按降序排列。 head -n1 选择第一行。您可以将其他文件添加到 files 变量中。

关于linux - 如何在使用 linux shell 时过滤多个文件并消除重复条目以选择单个条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35342866/

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