-2

File A is the file I have to search through. File B is the File, that has what I need to search in file A.

File A is a lot of code, simular to this:

test test 
a = rr
b = gg
test test
c = a + b
test 

Those variables are sometimes only declared, and not used, and I filtered all variables, that I need out, into File B:

a
b

I want to count how often each variable (each variable in File B has its own line, and is a string), appears in File B.

I would use count if in Excel to get this going, but I don´t know how to do something simular in Bash.

I tried to use grep -wc File_B File_A But this did not work for me, I guess its because this try´s to find the complete File_B in File_A, but that does not work.

Any help would be apprechiated.

Kind regards

Elias

4
  • Do you want a separate count per string to search for or do you want a total count? And what you search for is it literal strings or regular expressions? Please edit your question to specify this (do not give this information in comments). If you want separate counts grep is not the right tool; try awk. If you want a total count try grep -f File_B -wc Fila_A. If what you search for is literal strings (not regular expressions) add the -F option: grep -f File_B -Fwc Fila_A. Jul 8, 2022 at 5:42
  • Please clearly explain if you want a separate count or a global one. And please also show your expected output for your example. Jul 8, 2022 at 6:05
  • @Elias: So, you don't have any comment lines in File_A? Jul 8, 2022 at 7:08
  • Yes, but I removed those previous, would comments be a problem?
    – Elias
    Jul 8, 2022 at 7:36

1 Answer 1

2

The command grep -wc File_B File_A actually searches for the word File_B in the file File_A.

What you may have wanted is grep -wcf File_B File_A - the -f means "don't search for the pattern File_B, instead read the patterns to search from File_B.

The thing is, that will count all the matches from File_B that are found in File_A and sum it for you, so if the pattern on the first line of File_B is found 8 times, and the pattern on the second line of File_B is found 4 times, grep -wcf will just print 12. If you want to have an output that lists how many times each pattern was found, you'd want to write a loop that reads each line from File_B and greps for it individually.

Maybe something like this:

while read pat; do echo -n "$pat: "; grep -wc "$pat" File_A; done < File_B
4
  • Thanks, that worked perfektly, I think I am able to work with that. Thanks
    – Elias
    Jul 8, 2022 at 6:26
  • Perhaps the option -F would also make sense. At least it would speed up things a bit. Jul 8, 2022 at 7:08
  • I now have a bit of a different problem, when I run the code you added in the edit, the output is variable: 1. I would like to sort this in descending order. Is there a way to do this?
    – Elias
    Jul 8, 2022 at 7:34
  • I now used awk to change both columns, and was able to sort it in descending order.
    – Elias
    Jul 8, 2022 at 8:14

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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