Skip to main content
added 164 characters in body
Source Link
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.

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".

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.

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.

modify regexp to match only the words Orange and orange
Source Link
thanasisp
  • 8.5k
  • 2
  • 29
  • 40

With GNU grep:

grep -w -o -iwo "orange"'[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 -i makes it case-insensitive, so it'llexpression will match Orange as well. (If you don't want that, take out the -i"Orange" or "orange".)

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.

With GNU grep:

grep -w -o -i "orange" 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 -i makes it case-insensitive, so it'll match Orange as well. (If you don't want that, take out the -i.)

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.

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".

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.

Source Link
frabjous
  • 9.2k
  • 1
  • 38
  • 34

With GNU grep:

grep -w -o -i "orange" 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 -i makes it case-insensitive, so it'll match Orange as well. (If you don't want that, take out the -i.)

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.