Skip to main content
corrections
Source Link
Kannan Mohan
  • 3.3k
  • 2
  • 20
  • 16

We can use grep with regex to make it more simple and powerful.

To count specific character.

$ grep -o '"' file.txt|wc -l

To count special characters including whitespace characters.

$ grep -Po '[\S\s]''[\W_]' file.txt|wc -l

$ echo 'abcde'|grep -Po '[\S\s]' |wc -l HereHere we are selecting any character with '[\S\s]'[\S\s] and with -o option we make grep to print each match (which is, each character) in separate line. And then use wc -l to count each line.

We can use grep with regex to make it more simple and powerful.

$ grep -Po '[\S\s]' file.txt|wc -l

$ echo 'abcde'|grep -Po '[\S\s]' |wc -l Here we are selecting any character with '[\S\s]' and with -o option we make grep to print each match (which is, each character) in separate line. And then use wc -l to count each line.

We can use grep with regex to make it more simple and powerful.

To count specific character.

$ grep -o '"' file.txt|wc -l

To count special characters including whitespace characters.

$ grep -Po '[\W_]' file.txt|wc -l

Here we are selecting any character with [\S\s] and with -o option we make grep to print each match (which is, each character) in separate line. And then use wc -l to count each line.

Source Link
Kannan Mohan
  • 3.3k
  • 2
  • 20
  • 16

We can use grep with regex to make it more simple and powerful.

$ grep -Po '[\S\s]' file.txt|wc -l

$ echo 'abcde'|grep -Po '[\S\s]' |wc -l Here we are selecting any character with '[\S\s]' and with -o option we make grep to print each match (which is, each character) in separate line. And then use wc -l to count each line.