Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Calculate the value of the second column according to the same item in the first example

Shulou Source: shulou.com Published: 2022-06-03 06:33:09 11月23日 Update

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

Tags: Processing same content data file text method Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Microsoft Docker OPPO Reno Redmi