LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Grep regex: bracket (https://www.linuxquestions.org/questions/linux-newbie-8/grep-regex-bracket-4175601140/)

fanoflq 03-05-2017 07:15 PM

Grep regex: bracket
 
Code:

#First grep result using regex '[c]ron', quotes not included.
#I got ONE result
 ~ $ ps -ef | grep '[c]ron'
root      877    1  0 Mar04 ?        00:00:00 /usr/sbin/cron -f

#Second grep result using regex 'cron', quotes not included.
#I got TWO results!
 ~ $ ps -ef | grep 'cron'                                                                                       
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

Quote:

From man 7 regex:
A bracket expression is a list of characters enclosed in "[]".
It normally matches any single character from
the list
(but see below). If the list begins with '^',
it matches any single character (but see below) not
from the rest of the list. If two characters in the list
are separated by '-', this is shorthand for the full
range of characters between those two (inclusive)
in the collating sequence, for example, "[0-9]" in ASCII
matches any decimal digit. It is illegal(!) for two ranges
to share an endpoint, for example, "a-c-e".
Ranges are very collating-sequence-dependent, and
portable programs should avoid relying on them.
Why do I get different results?

chrism01 03-05-2017 10:57 PM

I admit I looked this one up https://unix.stackexchange.com/quest...rom-ps-results :)

fanoflq 03-05-2017 11:10 PM

Quote:

Originally Posted by chrism01 (Post 5679625)


Thanks.
I still do not get it.
Can you elaborate very sloooowly?

Turbocapitalist 03-05-2017 11:27 PM

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.

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.

tldr; you've made a pattern that does not match itself by using a set

fanoflq 03-06-2017 12:20 AM

Quote:

Originally Posted by Turbocapitalist (Post 5679633)
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.

Turbocapitalist 03-06-2017 12:27 AM

Quote:

Originally Posted by fanoflq (Post 5679647)
You made a mistake with [\]]ron, the "inside" closing bracket should be reposition like so:
...

How do you prevent conversion of ":<NO_SPACE_HERE>p" into smiley icon when we submit this post?
Thank you.

Good catch. I should have tested more closely.

There are two ways to avoid the smilies. One is to put an emtpy tag pair inbetween them.

:[b][/b]P

But that's not efficient. Another way is to look in the section under the text box, below the Submit Reply button in the "Additional Options" menu. There is a heading "Miscellaneous Options" with a check box for "Disable smilies in text"

fanoflq 03-06-2017 01:03 AM

Thank you Turbocapitalist!


All times are GMT -5. The time now is 04:29 AM.