Grep command to show the number of results

Hi
I wanted to know if there is an option in grep command to show the number of results (not the number of lines of findings).

Thanks

do you mean the count of matching lines? If so, then -c option will do it. From man page on a NetBSD system:

-c, --count
              Suppress  normal output; instead print a count of matching lines
              for each input file.  With the -v,  --invert-match  option  (see
              below), count non-matching lines.
~ grep --version
grep (GNU grep) 2.5.1a nb1

Hello milhan,

If you see OP's question carefully, OP needs to know total number of matched count of string NOT number of lines and as per your posted quote seems -c Option gives matched number of lines.

Take an example where a line contains a string in 5 lines but one line is having string 2 in a line then count should come 2 but this will not be the case as per man page lines you showed, though I can't test it as I am not in box as of now.

IMHO awk could be a choice for this one.

Thanks,
R. Singh

1 Like

Hi
maybe a pipe?

grep -o 'pattern' file | wc -l

instead of the "wc -l" command, you can use "cat -n" or "nl"

Number of lines? Is the -n option. According to my interpretation.
An example can clarify things...

Some Sunday jokes
if the file is not large

sed -z 's/<pattern>/\r/g; s/[^\r]//g;s/.*/expr length &/e' file

otherwise

sed -n 's/<pattern>/\r/g; s/[^\r]//g; /\r/H; ${x;s/\n//g; s/.*/expr length &/ep}' file
sed -n 's/<pattern>/\r/g; s/[^\r]//g; /\r/H; ${x;s/\r/+1/g; s/.*/echo $((&))/ep}' file
1 Like

Suppose you wanted to count the number of times the word of occurs in a file

Owner@Owner-PC ~
$ cat filename
all of the kings of Norway

Owner@Owner-PC ~
$ grep -Fo 'of'  filename |wc -l
2
1 Like

Hey nez,

How about some Monday fun :cool:

echo "all of the kings of Norway" | awk '{sum+=gsub(/of/,"&")} END{print sum}'

Taken example from Jim's sample echo message, cheers.

Thanks,
R. Singh

1 Like

Print count of case insensitive pattern /it/

echo "It was the best of times, it was the worst of times, it was the age of wisdom, it was the age of foolishness..." | 
perl -nle '$c+=()=/it/gi; END{print $c}'

Output:

4
1 Like

Why not

awk 'END {print gsub(/of/, "&")}' RS='\0' file

EDIT: fell into the same trap as did nezabudka below: RS=\0 splits on empty lines. Use other non-text chars instead, e.g. \001 or \377 ...

1 Like

could be and so

awk 'END {print NR-1}' RS='pattern' file

--- Post updated at 14:00 ---

awk -vFPAT='pattern' '{print NF}' RS='' file

--- Post updated at 14:05 ---

or even so

awk -vFPAT='pattern' '$0=NF' RS='' file

I must admit, I like jim mcnamara's neat response. It made me wonder about the string often all of the kinds of Norway as an example where the string might exist embedded in another word. Is this a problem to you?

By adding the -w flag, you can adjust the output:-

$ cat 283489
often all of the kings of Norway

$ grep -Fo 'of' 283489 | wc -l
3

$ grep -Fow 'of' 283489 | wc -l
2

Of course, this may or may not be useful, but I thought I should add it into the mix.

Kind regards,
Robin

1 Like

Be aware that FPAT is available in gawk only. Also, the RS='' will split the file on empty lines, so use this only if the file is adequately structured.

1 Like
sed '
:1
s/<pattern>/\r/g
s/[^\r]//g
$!{N;b1}
x
s/$/0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25/
T
:2
x
s/^[0-9\r][0-9]* \?//
t2
x
s/ .*//
' file

no matches - zero
empty line - counter overflow

1 Like

Using Python 2.7.x and 3.7.x...

#!/bin/bash
# inline.sh
echo "It was the best of times, it was the worst of times, it was the age of wisdom, it was the age of foolishness..." > /tmp/text

python2.7 -c "with open('/tmp/text') as FD:"$'\n\t'"contents = FD.read();"$'\n\t'"count = contents.count(' of ');"$'\n\t'"print(count)"

python3.7 -c "with open('/tmp/text') as FD:"$'\n\t'"contents = FD.read();"$'\n\t'"count = contents.count(' of ');"$'\n\t'"print(count)"

Results Linux Mint 19, default terminal.

bazza@amiga-MacBookPro:~$ cd Desktop/Code/Shell
bazza@amiga-MacBookPro:~/Desktop/Code/Shell$ chmod 755 inline.sh
bazza@amiga-MacBookPro:~/Desktop/Code/Shell$ ./inline.sh
4
4
bazza@amiga-MacBookPro:~/Desktop/Code/Shell$ _