linux 中给文本按照指定列标记重复次数
001、 awk + 数组实现
[root@pc1 test2]# ls a.txt [root@pc1 test2]# cat a.txt ## 测试数据 a b b a b c f f b a [root@pc1 test2]# awk '{ay[$0]++; print ay[$0], $0}' a.txt ## 按照列标记重复次数 1 a 1 b 2 b 2 a 3 b 1 c 1 f 2 f 4 b 3 a

02、python实现
[root@pc1 test1]# ls a.txt test.py [root@pc1 test1]# cat a.txt ## 测试数据 a b b a b c f f b a [root@pc1 test1]# cat test.py ## 程序 #!/usr/bin/env python3 # -*- coding: utf-8 -*- from collections import defaultdict dict1 = defaultdict(int) in_file = open("a.txt", "r") for i in in_file: i = i.strip() dict1[i] += 1 print(dict1[i], i) in_file.close() [root@pc1 test1]# python3 test.py ## 执行程序 1 a 1 b 2 b 2 a 3 b 1 c 1 f 2 f 4 b 3 a

。

浙公网安备 33010602011771号