我有以下文件:
userID PWD_HASH
test 1234
admin 1234
user 6789
abcd 5555
efgh 6666
root 1234
使用AWK,
我需要找到原始行及其行号重复的地方,
这样得到的输出如下:
NR $0
1 test 1234
2 admin 1234
6 root 1234
我已经尝试了以下操作,但是它没有使用NR打印正确的行号:
awk 'n=x[$2]{print NR" "n;print NR" "$0;} {x[$2]=$0;}' file.txt
任何帮助,将不胜感激!
解决方法:
$awk '
($2 in a) { # look for duplicates in $2
if(a[$2]) { # if found
print a[$2] # output the first, stored one
a[$2]="" # mark it outputed
}
print NR,$0 # print the duplicated one
next # skip the storing part that follows
}
{
a[$2]=NR OFS $0 # store the first of each with NR and full record
}' file
输出(带有文件头):
2 test 1234
3 admin 1234
7 root 1234