1

I am using

grep -o <string> <filename> | wc -l

to count the number of times <string> occurs in the file. I now need to extend this such that the string is from a list of items in another file.

The objective being to count the occurrences of each line of the 'string' file in the main file. The string file will be a list of VLANs and the main file(s) an output of the MAC address list of a large LAN Switch.

String file is in the format...

100 101 103 110 230

(Note spaces are in place before and after the numbers to cover off the string matching larger numbers and being found in other data (see below).

The files being checked against have lines in the format...

6c4b-904b-0c5c 230 Learned BAGG103 Y

I am ultimately looking to...

  1. Count the number of occurences of the string in the first file in the second, matches are expected against the second column
  2. Highlight any situations where the string does not match with the string listed

Edit - I think I have the first question answered but can't cover off listing the VLANs that aren't found.

  • Done - thanks for the advise – stranksa Dec 22 '19 at 14:26
3

The command you are asking for is simply:

grep -c -o -f strings_file input_file

The -f option will obtain the patterns from the strings_file, one per line.

The -c option will return the count of matches, thus eliminating the usage of | wc -l.

| improve this answer | |
  • Thanks for the response. Perhaps I worded the question badly. The output I am looking for is the number of occurrences of each string in the first file checking through the second. I found 'fgrep -of 1stFile 2ndFile | sort -h | uniq -c' from a search on here and that works a treat! – stranksa Dec 22 '19 at 14:14

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.