Awk:在第3个字段中查找重复项包括原始文件

时间:2022-04-22 18:45:38

I figured out the following code to find duplicate UIDs in a passwd file, but it doesn't include the first instance (the one that was later duplicated), I ultimately wanted to have a dictionary with UID = [ USER1, USER2 ] but I am not sure how to get it done in Awk.

我想出了以下代码来找到passwd文件中的重复UID,但它不包括第一个实例(后来重复的那个),我最终想要一个UID = [USER1,USER2]的字典但是我我不确定如何在Awk中完成它。

What I have so far:

到目前为止我所拥有的:

awk -F':' '$1 !~ /^#/ &&  _[$3]++ {print}' /etc/passwd  

Explanation (as I understand it), if regex matches a line not beginning with comment '#', then increment an array based on the current line UID value which makes that line become a non-zero/True value thus printing it.

解释(据我所知),如果正则表达式匹配不以注释'#'开头的行,则根据当前行UID值递增数组,使该行变为非零/ True值,从而打印它。

1 个解决方案

#1


This may help you to do it. First we save in an array the data, and in the END{} block we print all repeated lines in the array (also you have a print in execution time). Hope it helps you

这可能会帮助你做到这一点。首先我们在数组中保存数据,在END {}块中我们打印数组中的所有重复行(在执行时也有打印)。希望它能帮到你

awk -F":" '
    $1 !~ /^#/ && (counter[$3]>0) {a++;print "REPEATED|UID:"$3"|"$0"|"LastReaded[$3]; repeateds["a"a]=$0; repeateds["b"a]=LastReaded[$3]}
    $1 !~ /^#/ { counter[$3]++; LastReaded[$3]=$0} 
    END {for (i in repeateds)
         {
            print i"|"repeateds[i]
         }
    }
' /etc/passwd  

REPEATED|UID:229|pepito:*:229:229:pepito:/var/empty:/usr/bin/false|_avbdeviced:*:229:-2:Ethernet AVB Device Daemon:/var/empty:/usr/bin/false
a1|pepito:*:229:229:pepito:/var/empty:/usr/bin/false
b1|_avbdeviced:*:229:-2:Ethernet AVB Device Daemon:/var/empty:/usr/bin/false

#1


This may help you to do it. First we save in an array the data, and in the END{} block we print all repeated lines in the array (also you have a print in execution time). Hope it helps you

这可能会帮助你做到这一点。首先我们在数组中保存数据,在END {}块中我们打印数组中的所有重复行(在执行时也有打印)。希望它能帮到你

awk -F":" '
    $1 !~ /^#/ && (counter[$3]>0) {a++;print "REPEATED|UID:"$3"|"$0"|"LastReaded[$3]; repeateds["a"a]=$0; repeateds["b"a]=LastReaded[$3]}
    $1 !~ /^#/ { counter[$3]++; LastReaded[$3]=$0} 
    END {for (i in repeateds)
         {
            print i"|"repeateds[i]
         }
    }
' /etc/passwd  

REPEATED|UID:229|pepito:*:229:229:pepito:/var/empty:/usr/bin/false|_avbdeviced:*:229:-2:Ethernet AVB Device Daemon:/var/empty:/usr/bin/false
a1|pepito:*:229:229:pepito:/var/empty:/usr/bin/false
b1|_avbdeviced:*:229:-2:Ethernet AVB Device Daemon:/var/empty:/usr/bin/false