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?
asd asd is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
5
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.
3
You provided command works in this way. * mean it print if “a” is just right on next of “p”.
But if “a” is not just concatenate with “p” on right side, it only print “P”.
Terminal
Hasnain Ahmed is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.