Counting regex pattern matches in one line using sed or grep?
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 You could use grep -o then pipe through wc -l : $ echo "123 123 123" | grep -o 123 | wc -l 3 Maybe you should convert spaces to newlines first: $ echo "1 1 2 2 2 5" | tr ' ' $'\n' | grep -c 2 3 Maybe below: echo "123 123 123" | sed "s/123 /123\n/g" | wc -l ( maybe ugly, but my bash fu