对文本文件进行排序&删除重复项

发布于 2024-09-08 08:59:28 字数 241 浏览 0 评论 0原文

我有一个很大的文本文件,每行都有 4 位代码和一些有关它们的信息。它看起来像这样:

3456 information
1234 info
2222 Some ohter info

我需要对此文件进行排序,因此代码在文件中按升序排列。另外,有些代码出现多次,所以我需要删除重复项。我可以使用 perl、awk 或其他脚本语言来完成此操作吗?

预先感谢,

-skazhy

I have a large text file with 4-digit codes and some information about them in every row. It looks something like this:

3456 information
1234 info
2222 Some ohter info

I need to sort this file, so the codes are in ascending order in the file. Also, some codes appear more than once, so I need to remove duplicates. Can I do this with perl, awk or some other scripting language?

Thanks in advance,

-skazhy

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萧瑟寒风 2024-09-15 08:59:28
sort happybirthday.txt | uniq

来自 IBM

Google 的第一个结果:unix 删除重复行

sort happybirthday.txt | uniq

From IBM.

1st result for Google: unix remove duplicate lines.

メ斷腸人バ 2024-09-15 08:59:28

您可以创建一个散列,然后逐行读取文件,对于

  • 在第一个空格处分割的
  • 每一行,检查刚刚分割的数字 val(0) 是否在散列中,
  • 如果不在散列中,则插入 val(1) ,该行的其余部分,进入带有键 val(0)
  • continue

的哈希值,然后将(排序的)哈希值打印到文件中。

You can create a hash then read the file in line by line and for each line

  • split at the first space
  • check if the val(0), the number that you just split, is in the hash
  • if not the insert the val(1), rest of the line, into the hash with a key val(0)
  • continue

Then print the (sorted) hash to the file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文