Monday, May 20, 2024
6
rated 0 times [  6] [ 0]  / answers: 1 / hits: 1801  / 2 Years ago, mon, august 8, 2022, 2:36:32

I want to know that how many times 'ABCD'(file A) comes in DB (file B). Likewise I want to know about every singe line present in file A against DB. I need an automated command which can ease my work because I have a large amount of data in file A and I want to search it against many databases. I just make the characters bold for understanding.



File A



ABCD
EFG
HIJKL
MNO
PQRSTU


File B




XYZABCDFORNTUFPSRWSABCFYWSZCFTHBFORTYBJNFABCDDEFGACVRTEFGPQRMNOOPQEFGZXXXYY




Desired output:



ABCD  2
EFG 3
HIJKL 4567
MNO 0
PQRSTU 7652

More From » command-line

 Answers
6

My suggestion is:



IFS=; while read -r word; do printf "%s " $word; grep -o $word b | wc -l; done < a



  • using while we loop into words (file a)

  • printf "%s " $word : prints the word name, eg: ABCD

  • grep -o $word b | wc -l: counts and print the number of occurrences


[#10948] Monday, August 8, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
icielight

Total Points: 158
Total Questions: 92
Total Answers: 93

Location: San Marino
Member since Thu, Jun 30, 2022
2 Years ago
;