31

I want to count the number of matches there is on one single line (or all lines as there always will be only one line).

I want to count not just one match per line as in

echo "123 123 123" | grep -c -E "123" # Result: 1

Better example:

echo "1 1 2 2 2 5" | grep -c -E '([^ ])( \1){1}' # Result: 1, expected: 2 or 3
3
  • is the data always space-separated? May 30, 2011 at 22:44
  • There will always only be "one data" because maybe i want to match 123 123 3 (or 2) times in 123 123 123 123
    – Tyilo
    May 30, 2011 at 22:55
  • 1
    +1 for the question, -1 for the bizarre regex example May 30, 2011 at 23:03

5 Answers 5

53

You could use grep -o then pipe through wc -l:

$ echo "123 123 123" | grep -o 123 | wc -l
3
10
  • 1
    My version of grep doesn't know what -o is :(
    – manojlds
    May 30, 2011 at 22:52
  • 15
    You need to ask Father Christmas for a new grep this year. :) May 30, 2011 at 22:54
  • @manojlds, do you have egrep? Same thing would work w/ egrep May 30, 2011 at 22:54
  • @Mike Pennington - thanks, egrep says the same. I am on Windows now, so i think it's expected.
    – manojlds
    May 30, 2011 at 22:57
  • @Tylio - that's not surprising, look at your regex. It's asking for 0 or more instances of anything other than a space, followed by a space, followed by the first thing you matched again. Note: 0 or more. There are indeed five such matches in your string (assuming you don't rewind after each match). They are: 1) "1 1" (bytes 1-3), 2) " " (i.e. zero instances of something that isn't a space, followed by a space, followed by the same zero instances again - byte 4), 3) "2 2" (bytes 5-7), 4) " " (byte 8) and finally 5) " " (byte 10). Phew! May 30, 2011 at 23:00
1

Maybe below:

echo "123 123 123" | sed "s/123 /123\n/g" | wc -l

( maybe ugly, but my bash fu is not that great )

2
  • @Tyilo - what did you try? I am getting 3 for the above input
    – manojlds
    May 30, 2011 at 22:58
  • Copy and pasted your code, but i remember now that my sed doesn't support \n
    – Tyilo
    May 30, 2011 at 22:59
1

Maybe you should convert spaces to newlines first:

$ echo "1 1 2 2 2 5" | tr ' ' $'\n' | grep -c 2
3
0

Why not use awk? You could use awk '{print gsub(your_regex,"&")}' to print the number of matches on each line, or awk '{c+=gsub(your_regex,"&")}END{print c}' to print the total number of matches. Note that relative speed may vary depending on which awk implementation is used, and which input is given.

1
  • Another way by gawk is gawk -v FPAT=your_regex '{print NF}' or gawk -v FPAT=your_regex '{c+=NF}END{print c}', respectively.
    – jarno
    Sep 3, 2015 at 17:57
0

This might work for you:

sed -n -e ':a' -e 's/123//p' -e 'ta' file | sed -n '$='

GNU sed could be written:

sed -n ':;s/123//p;t' file | sed -n '$='
1
  • The first script does't work by GNU sed 4.2.2: "sed: can't find label for jump to a'". It seems to work better, if you replace :ta` by :a. The scripts seems to require newline in the end of intput. Besides, the script outputs nothing, if no matches are found. Test: printf 123 | sed -n ':;s/123//p;t' | sed -n '$=' outputs nothing.
    – jarno
    Sep 4, 2015 at 18:09

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.