I would like see practical examples of the differences between -o and -c with grep command
echo a.b.c.d | grep -o “[.]” | wc -l
why does it give me output 3 and not 4 ? i have four dots
I would like see practical examples of the differences between -o and -c with grep command
echo a.b.c.d | grep -o “[.]” | wc -l
why does it give me output 3 and not 4 ? i have four dots
The -o
option shows only matched part in your regex/word. For example, in the following, if you want to grep xyz
word only, then you can do the following:
printf "abc xyz\nxyzzzz\nxyabc\nxyzabc\n" | grep -w -o 'xyz'
# this will grep all other lines matching xyz
printf "abc xyz\nxyzzzz\nxyabc\nxyzabc\n" | grep 'xyz'
The -c
option count matched string/regex etc. Do you want to count ‘xyz’ word in the string? Try
printf "abc xyz\nxyzzzz\nxyabc\nxyzabc\n" | grep -w -o -c 'xyz'
# this will grep all other lines matching xyz
printf "abc xyz\nxyzzzz\nxyabc\nxyzabc\n" | grep -c 'xyz'
I hope this helps.
I see that you attach option -w with -o , is it necessary ?
That is to make examples clear. It all depends upon what kind of output you want and what input is provided. I hope this helps.