1

I have two texts (frequency count dic) I want to increase the frequency count of the first file by adding only the same word in the second file.

for example my file1

100  man 
522  women  
600  kids 
600  football 

my file2

100 man 
300 women 
600 kids 
900 football
500 fifa 
500 world 

I want this output

200 man
822 women 
1200 kids
1500 football 

The words in the second file, that not exit in the first file will be remove.

500 fifa  # removed 
500 world # removed  

I tried with sed/cat and join cmd but I'm not getting the result I want

5

Here's an awk approach:

$ awk 'NR==FNR{a[$2]=$1; next}($2 in a){print $1+a[$2],$2}' file1 file2
200 man
822 women
1200 kids
1500 football

If you also want to include words that appear in file1 but not in file2, use:

awk 'NR==FNR{a[$2]=$1; next}{print $1+a[$2],$2}' file2 file1 
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.