我需要有关文本文件特定记录编号的帮助。 这是ldif文件,大约2GB。加工机器是unix所以我输入了awk。虽然已经尝试过nl和cat它看起来更复杂并且awk看起来很适合这个任务,但我对sql很熟悉,但这并不接近它: - )
目标是保证组和组元素的数字唯一性:
输入:
dn: uc=an
version: 12
dn: uid=fcb
uid: ljfhsfff
missdata: at12
missdata: at3
fladata: part2
fladata: part3
fladata: part1
dn: uid=fccb
uid: kjhfa8
missdata: at1
missdata: at8
missdata: at10
missdata: at14
fladata:: a06b6a==
fladata: part3
att: dsc
输出(可能之一):
1 1 dn: uc=an
1 1 version: 12
2 1 dn: uid=fcb
2 1 uid: ljfhsfff
2 1 missdata: at12
2 2 missdata: at3
2 1 fladata: part2
2 2 fladata: part3
2 3 fladata: part1
3 1 dn: uid=fccb
3 1 uid: kjhfa8
3 1 missdata: at1
3 2 missdata: at8
3 3 missdata: at10
3 4 missdata: at14
3 1 fladata:: a06b6a==
3 2 fladata: part3
3 1 att: dsc
答案 0 :(得分:2)
$ awk -F':' '{if (NF) {$0 = (grpNr+1) OFS (++eltCnt[$1]) OFS $0} else {++grpNr; delete eltCnt}} 1' file
1 1 dn: uc=an
1 1 version: 12
2 1 dn: uid=fcb
2 1 uid: ljfhsfff
2 1 missdata: at12
2 2 missdata: at3
2 1 fladata: part2
2 2 fladata: part3
2 3 fladata: part1
3 1 dn: uid=fccb
3 1 uid: kjhfa8
3 1 missdata: at1
3 2 missdata: at8
3 3 missdata: at10
3 4 missdata: at14
3 1 fladata:: a06b6a==
3 2 fladata: part3
3 1 att: dsc