gpt4 book ai didi

linux - 使用 Bash 循环遍历 txt 文件中的唯一行

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:36:06 25 4
gpt4 key购买 nike

我正在遍历 txt 文件中以制表符分隔的行。此 txt 文件是 xml/xslt 进程的输出并且有重复项。下面我正在寻找 txt 文件的解决方案,但使用 XSLT 的解决方案同样值得赞赏。请参阅示例 txt 文件。

txtfile.txt:第 3 行与第 1 行重复

hello@example.com  running  1111
puppy@kennel.com running 9876
hello@example.com running 1111
husky@siberia.com shutdown 1234
puppy@kennel.com running 9876
hello@example.com running 1111

我的问题是: 是否可以在循环中跳过重复的行,以便循环只处理唯一的行?在这种情况下,如何配置循环第1、2、4行并跳过第3、5、6行?

我当前读取重复项的工作循环:

while read name status num
do
echo "<tag1>"
echo "<tag2>"$name"</tag2>"
echo "<tag3>"$status"</tag3>"
echo "<tag2>"$num"</tag2>"
echo "</tag1>"

done < txtfile.txt

在我的 txt 文件中有数百行,将近一半是重复的,所以这对我来说是个大问题!任何想法/解决方案表示赞赏。提前致谢。

最佳答案

您可以通过 sort -u 读取该文件以消除重复行:

sort -u /your/file | while read ...

关于linux - 使用 Bash 循环遍历 txt 文件中的唯一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31148732/

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