如何使用for循环删除重复bash中第一个数字的行?(How to use a for loop to remove lines that repeat the first number in bas

编程入门 行业动态 更新时间:2024-10-12 22:33:22
如何使用for循环删除重复bash中第一个数字的行?(How to use a for loop to remove lines that repeat the first number in bash?)

通过运行find /mnt/ExtraHDD1/ -type f -links +1 -printf '%i %n %p\n' | sort -n > test.txt find /mnt/ExtraHDD1/ -type f -links +1 -printf '%i %n %p\n' | sort -n > test.txt ,我得到以下输出:

32113282 3 /mnt/ExtraHDD1/1/6.png 32113282 3 /mnt/ExtraHDD1/2/6.png 32113282 3 /mnt/ExtraHDD1/3/6.png 32113284 2 /mnt/ExtraHDD1/1/8.png 32113284 2 /mnt/ExtraHDD1/2/8.png

我运行的命令列出了/ mnt / ExtraHDD1 /中的所有硬链接。 我正在尝试生成排除列表,以便我不会多次将相同的文件上传到云端。 在每一行中,第一个数字(例如32113282)表示inode编号。 第二个数字(例如3)表示文件硬链接的次数。 由于这是一个排除列表,我想删除每个inode号的第一个实例,以便排除除第一个副本之外的所有硬链接副本。 所以在这种情况下,我想要以下输出:

32113282 3 /mnt/ExtraHDD1/2/6.png 32113282 3 /mnt/ExtraHDD1/3/6.png 32113284 2 /mnt/ExtraHDD1/2/8.png

如何在bash中使用for循环执行此操作? 我知道如何用Java实现它,但bash语法让我感到困惑。

By running find /mnt/ExtraHDD1/ -type f -links +1 -printf '%i %n %p\n' | sort -n > test.txt, I get the following output:

32113282 3 /mnt/ExtraHDD1/1/6.png 32113282 3 /mnt/ExtraHDD1/2/6.png 32113282 3 /mnt/ExtraHDD1/3/6.png 32113284 2 /mnt/ExtraHDD1/1/8.png 32113284 2 /mnt/ExtraHDD1/2/8.png

The command I ran lists all the hard-links in /mnt/ExtraHDD1/. I'm trying to generate an exclude list so that I don't upload the same file multiple times to the cloud. In each line, the first number (e.g. 32113282) represents the inode number. The second number (e.g. 3) represents the number of times the file is hard-linked. Since this is an exclude list, I want to remove the first instance of each inode number so that I exclude all copies of a hard-link except the first copy. So in this case, I want the following output:

32113282 3 /mnt/ExtraHDD1/2/6.png 32113282 3 /mnt/ExtraHDD1/3/6.png 32113284 2 /mnt/ExtraHDD1/2/8.png

How can I do this using a for loop in bash? I know how to do it in Java but bash syntax is confusing me.

最满意答案

拯救!

如果我理解正确,第一个字段是关键。

$ awk 'a[$1]++' file 32113282 3 /mnt/ExtraHDD1/2/6.png 32113282 3 /mnt/ExtraHDD1/3/6.png 32113284 2 /mnt/ExtraHDD1/2/8.png

awk to the rescue!

If I understand correctly, first field is the key.

$ awk 'a[$1]++' file 32113282 3 /mnt/ExtraHDD1/2/6.png 32113282 3 /mnt/ExtraHDD1/3/6.png 32113284 2 /mnt/ExtraHDD1/2/8.png

更多推荐

本文发布于:2023-08-04 20:53:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1422666.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   数字   bash   loop   number

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!