Awk:在第三个字段中找到重复项,包括原始字段
Awk: find duplicates in 3rd field INCLUDING original
我想出了下面的代码来在 passwd 文件中查找重复的 UID,但它不包括第一个实例(后来重复的那个),我最终想要一个字典 UID = [ USER1, USER2 ]
但我不确定如何在 Awk 中完成它。
我目前拥有的:
awk -F':' ' !~ /^#/ && _[]++ {print}' /etc/passwd
解释(据我所知),如果正则表达式匹配不以注释“#”开头的行,则根据当前行 UID 值递增一个数组,这使得该行成为 non-zero/True 值,因此正在打印。
这可能会帮助你做到这一点。首先,我们将数据保存在一个数组中,然后在 END{} 块中,我们打印数组中所有重复的行(执行时也有打印)。希望对你有帮助
awk -F":" '
!~ /^#/ && (counter[]>0) {a++;print "REPEATED|UID:""|"[=10=]"|"LastReaded[]; repeateds["a"a]=[=10=]; repeateds["b"a]=LastReaded[]}
!~ /^#/ { counter[]++; LastReaded[]=[=10=]}
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
我想出了下面的代码来在 passwd 文件中查找重复的 UID,但它不包括第一个实例(后来重复的那个),我最终想要一个字典 UID = [ USER1, USER2 ]
但我不确定如何在 Awk 中完成它。
我目前拥有的:
awk -F':' ' !~ /^#/ && _[]++ {print}' /etc/passwd
解释(据我所知),如果正则表达式匹配不以注释“#”开头的行,则根据当前行 UID 值递增一个数组,这使得该行成为 non-zero/True 值,因此正在打印。
这可能会帮助你做到这一点。首先,我们将数据保存在一个数组中,然后在 END{} 块中,我们打印数组中所有重复的行(执行时也有打印)。希望对你有帮助
awk -F":" '
!~ /^#/ && (counter[]>0) {a++;print "REPEATED|UID:""|"[=10=]"|"LastReaded[]; repeateds["a"a]=[=10=]; repeateds["b"a]=LastReaded[]}
!~ /^#/ { counter[]++; LastReaded[]=[=10=]}
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