Quote:
Originally Posted by Turbocapitalist
You get different results because one is a pattern and the other is a string.
Both instances of grep are looking for a regex pattern among strings produces by ps.
Since that string is present in the lines for PID 877 and 26692, they will both be printed.
Code:
root 877 1 0 Mar04 ? 00:00:00 /usr/sbin/cron -f
user1 26692 11882 0 18:03 pts/2 00:00:00 grep --color=auto cron
Using the pattern [c]cron with grep, you'll find the following in your process list (if you could look at the right moment).
Code:
user1 26693 11882 0 18:03 pts/2 00:00:00 grep --color=auto [c]ron
That string doesn't match the pattern used in grep. The pattern only finds cron no square brackets. So 26693 is not shown. If you were to try [abcde]ron or [[:alpha:]]ron, you'd still find 26692 and not 26693.
tldr; you've made a pattern that does not match itself by using a set
|
OK I now get it.
Here is how I understood what you said.
When we do this:
Code:
~ $ ps -ef | grep "[[:punct:]]ron"
It prints out (as one of its many lines for all the processes) the grep process and its arguments.
Code:
user1 9197 8355 0 22:45 pts/1 00:00:00 grep --color=auto [[:punct:]]ron
In this case the grep command is actually an alias, grep --color=auto.
Likewise when we do this (as you have mentioned):
Code:
~ $ ps -ef | grep "[c]ron"
Two lines of interests (plus many lines for other processes)
were piped into grep, and they are:
Code:
... ...
root 861 1 0 18:47 ? 00:00:00 /usr/sbin/cron -f
.... ....
user1 9197 8355 0 22:45 pts/1 00:00:00 grep --color=auto [c]ron
... ...
But the match regex match pattern for grep is "[c]ron".
However regex "[c]ron", quotes excluded, only match string pattern "cron".
And there is no "cron" string in ps output string below:
Code:
user1 9197 8355 0 22:45 pts/1 00:00:00 grep --color=auto [c]ron
Which is why it did not show up as a grep output.
You wrote this:
Code:
However, if you were to try [\]]ron or [[: punct :]]ron, you'd find 26693. But note carefully what the colors show as the matched pattern. If you want the whole [c]ron thing you'd need a broader pattern like [c[: punct :]]ron to find it.
You made a mistake with [\
]]ron, the "inside" closing bracket should be reposition like so:
Code:
~ $ ps -ef | grep "[]\]ron"
user1 9756 8355 0 23:12 pts/1 00:00:00 grep --color=auto []\]ron
How do you prevent conversion of ":<NO_SPACE_HERE>p" into smiley icon when we submit this post?
Thank you.