The most easiest way to count the number of lines, words, and characters in text file is to use the Linux command “wc” in terminal. The command “wc” basically means “word count” and with different optional parameters one can use it to count the number of lines, words, and characters in a text file.
First, we split the string by spaces in a. Then, take a variable count = 0 and in every true condition we increment the count by 1. Now run a loop at 0 to length of string and check if our string is equal to the word.
Using grep -c alone will count the number of lines that contain the matching word instead of the number of total matches. The -o option is what tells grep to output each match in a unique line and then wc -l tells wc to count the number of lines. This is how the total number of matching words is deduced.
Using the grep Command And by using the -o option, it prints only the matching character. Then we sort results from the grep command. And we count the occurrences of the search character using the uniq command with the -c option. Finally, we use the cut command to remove the unwanted string from the final result.
How about this:
fgrep -o f <file> | wc -l
Note: Besides much easier to remember/duplicate and customize, this is about three times (sorry, edit! botched the first test) faster than Vereb's answer.
even faster:
tr -cd f < file | wc -c
Time for this command with a file with 4.9 MB and 1100000 occurences of the searched character:
real 0m0.089s
user 0m0.057s
sys 0m0.027s
Time for Vereb answer with echo
, cat
, tr
and bc
for the same file:
real 0m0.168s
user 0m0.059s
sys 0m0.115s
Time for Rob Hruska answer with tr
, sed
and wc
for the same file:
real 0m0.465s
user 0m0.411s
sys 0m0.080s
Time for Jefromi answer with fgrep
and wc
for the same file:
real 0m0.522s
user 0m0.477s
sys 0m0.023s
echo $(cat <file> | wc -c) - $(cat <file> | tr -d 'A' | wc -c) | bc
where the A is the character
Time for this command with a file with 4.9 MB and 1100000 occurences of the searched character:
real 0m0.168s
user 0m0.059s
sys 0m0.115s
If all you need to do is count the number of lines containing your character, this will work:
grep -c 'f' myfile
However, it counts multiple occurrences of 'f' on the same line as a single match.
tr -d '\n' < file | sed 's/A/A\n/g' | wc -l
Replacing the two occurrences of "A" with your character, and "file" with your input file.
tr -d '\n' < file
: removes newlinessed 's/A/A\n/g
: adds a newline after every occurrence of "A"wc -l
: counts the number of linesExample:
$ cat file
abcdefgabcdefgababababbbba
1234gabca
$ tr -d '\n' < file | sed 's/a/a\n/g' | wc -l
9
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With