Skip to main content
3 of 3
added 164 characters in body
frabjous
  • 9.2k
  • 1
  • 38
  • 34

With GNU grep:

grep -wo '[Oo]range' filename | wc -l

Here the -w makes it only match whole words; the -o makes it split each occurrence of a match on the same line into its own line and suppresses other output, and the expression will match "Orange" or "orange".

(If you want it to be wholly case-insensitive, and also match "ORANGE" and "OraNGe", etc., you can add the -i flag and simply use 'orange' for the pattern.)

This is then passed to wc which counts the words. Since each is on its own line you could use wc -l or wc -w and the results will be the same.

frabjous
  • 9.2k
  • 1
  • 38
  • 34