LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-05-2017, 08:15 PM   #1
fanoflq
Member
 
Registered: Nov 2015
Posts: 397

Rep: Reputation: Disabled
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?
 
Old 03-05-2017, 11:57 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,415

Rep: Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785Reputation: 2785
I admit I looked this one up https://unix.stackexchange.com/quest...rom-ps-results
 
1 members found this post helpful.
Old 03-06-2017, 12:10 AM   #3
fanoflq
Member
 
Registered: Nov 2015
Posts: 397

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by chrism01 View Post

Thanks.
I still do not get it.
Can you elaborate very sloooowly?
 
Old 03-06-2017, 12:27 AM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,598
Blog Entries: 4

Rep: Reputation: 3894Reputation: 3894Reputation: 3894Reputation: 3894Reputation: 3894Reputation: 3894Reputation: 3894Reputation: 3894Reputation: 3894Reputation: 3894Reputation: 3894
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
 
1 members found this post helpful.
Old 03-06-2017, 01:20 AM   #5
fanoflq
Member
 
Registered: Nov 2015
Posts: 397

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Turbocapitalist View Post
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.

Last edited by fanoflq; 03-06-2017 at 02:02 AM.
 
Old 03-06-2017, 01:27 AM   #6
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,598
Blog Entries: 4

Rep: Reputation: 3894Reputation: 3894Reputation: 3894Reputation: 3894Reputation: 3894Reputation: 3894Reputation: 3894Reputation: 3894Reputation: 3894Reputation: 3894Reputation: 3894
Quote:
Originally Posted by fanoflq View Post
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"
 
1 members found this post helpful.
Old 03-06-2017, 02:03 AM   #7
fanoflq
Member
 
Registered: Nov 2015
Posts: 397

Original Poster
Rep: Reputation: Disabled
Thank you Turbocapitalist!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Using grep in perl with regex. jags1984 Linux - Newbie 3 11-10-2014 06:48 AM
REGEX with grep Anna1987 Linux - Newbie 9 03-09-2013 08:56 AM
[SOLVED] Combining regex in grep devUnix Linux - General 2 09-06-2011 12:11 PM
[SOLVED] grep Bracket Expressions Star_Gazer Linux - Newbie 2 04-10-2010 09:30 PM
regex in ls vs. grep jhwilliams Linux - Software 2 08-10-2007 11:14 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:29 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration