Search code examples
regexgrep

Why does the grep command select more than expected?


When i write below command, it selects "aapp". but it should be "aap". Because '*' means zero or more 'a'.'p' means one p.

$ echo "aapple" | grep  "a*p"

terminal screenshot

can somebody please explain the reason, why the grep not select as expected?


Solution

  • Grep is line oriented, so it reads lines from input, and prints the entire line when any part of it matches the specified RE. Thus any input line containing a p will be printed with a*p.

    $ echo "aapple" | grep "a*p"
    aapple
    $ echo "nothing special" | grep "a*p"
    nothing special
    

    If you only want to print the part that matches the RE, you can use the -o option, but then it will print multiple lines if the RE matches in multiple non-overlapping places:

    $ echo "aapple" | grep  -o "a*p"
    aap
    p
    

    If you use the --color option, it will print these two parts of the line in red, giving what you see in your image.