40

I have a few text files and I'd like to count how many times a letter appears in each?

Specifically, I'd like to use the UNIX shell to do this, in the form of: cat file | .... do stuff...

Is there a way I can get the wc command to do this?

2
  • cat file | is a no go, no do, do not even think of it. Mostly it is used with less or grep, where the cat is not needed. Please do not use cat if you dont want to simply output the file. Sep 2, 2009 at 16:00
  • 2
    cat is actually useful for keeping RAM usage at a minimum. It's useful when working with big files as it will feed in through the pipe line by line.
    – Goran
    Aug 22, 2013 at 2:43

10 Answers 10

47
grep char -o filename | wc -l
5
  • 10
    Note for those initially confused like me, replace "char" with the character or string you are looking for.
    – rlorenzo
    Sep 14, 2011 at 19:39
  • 2
    Also, most grep implementations have a "-c" option to count.
    – Michaël
    Feb 14, 2013 at 15:32
  • 1
    The above also works for strings, so grep <string> -o <file> | wc -l is the generic version. Eg: grep , -o myfile.txt | wc -l counts the number of commas in myfile.txt and grep abcd -o myfile.txt | wc -l counts the number of abcds in myfile.txt
    – arun
    Oct 9, 2013 at 23:59
  • 1
    How do I modify this if want to count number of (.)s periods/full stop characters? I want get approximate number of sentences in a document. Thanks! Apr 20, 2014 at 21:39
  • 9
    @Michaël grep -c doesn't count multiple occurrences on the same line, unfortunately
    – Camusensei
    Apr 7, 2016 at 11:59
13

Another alternative:

tr -d -C X <infile | wc -c

where X is the character or string of characters you want to count and infile is the input file.

2
  • 4
    This is cleaner than the accepted answer (which relies on the fact that the output of grep -o is newline-separated). This also works with ANY character (including \n)
    – Camusensei
    Apr 7, 2016 at 12:05
  • Interpretation: "delete (-d) everything except for characters of type X (-C), then count the number of remaining characters (wc -c)."
    – ijoseph
    Oct 6, 2019 at 17:17
1

Alternative to grep:

sed 's/[^x]//g' filename | tr -d '\012' | wc -c

where x is the character you want to count.

1

There's also awk:

$ echo -e "hello world\nbye all" | awk -Fl '{c += NF - 1} END {print c}'
5

Change -Fl to -F<your character>.

This works by setting the field delimiter to the character specified by -F, then accumulating the number of fields on each line - 1 (because if there's one delimiter, there are two fields - but we should only count 1).

1
awk '{ printf "%s\n", gsub( "ur_char", "oth_char", $0 ) }' < your_file_name > output.txt

you can add count of current line number to get the line numbers in awk also.

1

echo "a/b/c/d/e/f/g" | awk -F"/" '{print NF}'

this will give the number of occurrence of character "/"

0
1

In this case, i'am counting the character "|":

expr `wc -c < filename` \- `tr -d \| < filename | wc -c`
1

You can try easily: grep -c 'YOUR LETTER' YOUR FILE

2
  • the number will appear on the shell, in the newline Aug 21, 2017 at 13:59
  • 2
    will result in number of lines containing the character, and not number of character instances
    – roee
    Aug 5, 2018 at 15:38
0

try it with

grep  [PATTERN] -o [FILE] | wc -l

and please do not use cat if not needed.

2
  • 1
    it doesn't count multiple characters per line Sep 2, 2009 at 16:10
  • @samoz:cat is wrong, since it should input the read file to another program - the other program is able to read the file by itself, so the use of cat is unneeded and complicates the codeline. @SilentGhost: your right. Sep 2, 2009 at 16:25
0

Here is another way

cat  input_file | \
awk 'BEGIN {FS="x"; var=0 } \
{if (NF>0){ var=var + (NF-1) } } \
END{print var}'

where X is the character or string of characters you want to count and infile is the input file

0

Your Answer

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.