Calculate the value of the second column according to the same item in the first example
File test:
The text is as follows:
A 2
B 3
C 6
A 8
D 9
The processing method is that if the first column is the same, the values of the second column are added (for example, a 10). After processing, the data of the second column with a value greater than 8 is printed.
Awk:
Awk'{a [$1] + = $2;} END {for (i in a) {if (a [I] > 8) {print I "" a [I];} 'test
Python:
#! / usr/bin/env pythonf = file ("test",'r') m = {} for x in f.readlines (): key,value = x.split () if m.has_key (key): M [key] = int (m [key]) + int (value) else: M [key] = value if int (m [key]) < 8: del m [key] print mfor KJV in m.items (): print KJV