Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Grep expression help.
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
dE_logics
Advocate
Advocate


Joined: 02 Jan 2009
Posts: 2253
Location: $TERM

PostPosted: Wed Jul 30, 2014 8:33 am    Post subject: Grep expression help. Reply with quote

I dont understand, why does this command result in the following output --

Code:
echo adfwefasdfllfwefasdfurui | grep -E '[(hello)(world)]'
adfwefasdfllfwefasdfurui


[ ] means match any one of the expressions inside the brackets. Then I use parenthesis to group strings so [] treats it as a single character, matching hello | world.

But grep simply assumes the parenthesis to be one of the search terms.
_________________
My blog
Back to top
View user's profile Send private message
mvaterlaus
Apprentice
Apprentice


Joined: 01 Oct 2010
Posts: 234
Location: Switzerland

PostPosted: Wed Jul 30, 2014 8:48 am    Post subject: Reply with quote

hi,
I played around a bit, and it looks like, that the following will command will work:

Code:
echo adfweworldfasdfllfhellowefasdfurui | grep -E 'hello|world'


That is also, how I would have done it, since everything in the '...' part is interpreted as a POSIX regex (the -E switch).
_________________
For calming down your eyes or clearing your mind: www.patrickwehli.ch
Back to top
View user's profile Send private message
Atom2
Apprentice
Apprentice


Joined: 01 Aug 2011
Posts: 185

PostPosted: Wed Jul 30, 2014 10:40 am    Post subject: Re: Grep expression help. Reply with quote

dE_logics wrote:
I dont understand, why does this command result in the following output --

Code:
echo adfwefasdfllfwefasdfurui | grep -E '[(hello)(world)]'
adfwefasdfllfwefasdfurui


[ ] means match any one of the expressions inside the brackets. Then I use parenthesis to group strings so [] treats it as a single character, matching hello | world.

But grep simply assumes the parenthesis to be one of the search terms.
I see that you have solved your issue already. But to clear up any confusion let me cite from the man page (emphasis from me):
Quote:
A bracket expression is a list of characters enclosed by [ and ]. It matches any single character in that list; if the first character of the list is the caret ^ then it matches any character not in the list. For example, the regular expression [0123456789] matches any single digit.
Within a bracket expression (leaving aside predefined charcater classes within a bracket expression) only "^" (as the first character) and "-" (unless being last, being the only character, or being the only of two characters following the "^" character) serve as character-class meta-characters (i.e. those with special meaning). So in a nutshell the charcater class enclosed in brackets ([..]) always matches on a single character and never on a string of characters. Even
Code:
[01-38]
would not match on any two digit number between 01 and 38 but rather on any of the single digits 0, 1, 2, 3, and 8.

Regards Atom2
Back to top
View user's profile Send private message
dE_logics
Advocate
Advocate


Joined: 02 Jan 2009
Posts: 2253
Location: $TERM

PostPosted: Fri Aug 01, 2014 5:24 pm    Post subject: Re: Grep expression help. Reply with quote

Atom2 wrote:
dE_logics wrote:
I dont understand, why does this command result in the following output --

Code:
echo adfwefasdfllfwefasdfurui | grep -E '[(hello)(world)]'
adfwefasdfllfwefasdfurui


[ ] means match any one of the expressions inside the brackets. Then I use parenthesis to group strings so [] treats it as a single character, matching hello | world.

But grep simply assumes the parenthesis to be one of the search terms.
I see that you have solved your issue already. But to clear up any confusion let me cite from the man page (emphasis from me):
Quote:
A bracket expression is a list of characters enclosed by [ and ]. It matches any single character in that list; if the first character of the list is the caret ^ then it matches any character not in the list. For example, the regular expression [0123456789] matches any single digit.
Within a bracket expression (leaving aside predefined charcater classes within a bracket expression) only "^" (as the first character) and "-" (unless being last, being the only character, or being the only of two characters following the "^" character) serve as character-class meta-characters (i.e. those with special meaning). So in a nutshell the charcater class enclosed in brackets ([..]) always matches on a single character and never on a string of characters. Even
Code:
[01-38]
would not match on any two digit number between 01 and 38 but rather on any of the single digits 0, 1, 2, 3, and 8.

Regards Atom2


Thanks for clarifying that.
_________________
My blog
Back to top
View user's profile Send private message
dataking
Apprentice
Apprentice


Joined: 20 Apr 2005
Posts: 251

PostPosted: Fri Aug 01, 2014 5:38 pm    Post subject: Re: Grep expression help. Reply with quote

Atom2 wrote:
Code:
[01-38]
would not match on any two digit number between 01 and 38 but rather on any of the single digits 0, 1, 2, 3, and 8.

....and '-'.
_________________
-= the D@7@k|n& =-
Back to top
View user's profile Send private message
John R. Graham
Administrator
Administrator


Joined: 08 Mar 2005
Posts: 10590
Location: Somewhere over Atlanta, Georgia

PostPosted: Fri Aug 01, 2014 5:44 pm    Post subject: Re: Grep expression help. Reply with quote

dataking wrote:
....and '-'.
Incorrect. Inside brackets, the '-' is a range operator, not a member of the character class.

- John
_________________
I can confirm that I have received between 0 and 499 National Security Letters.
Back to top
View user's profile Send private message
dataking
Apprentice
Apprentice


Joined: 20 Apr 2005
Posts: 251

PostPosted: Fri Aug 01, 2014 5:47 pm    Post subject: Re: Grep expression help. Reply with quote

dE_logics wrote:
I dont understand, why does this command result in the following output --

Code:
echo adfwefasdfllfwefasdfurui | grep -E '[(hello)(world)]'
adfwefasdfllfwefasdfurui


[ ] means match any one of the expressions inside the brackets. Then I use parenthesis to group strings so [] treats it as a single character, matching hello | world.

But grep simply assumes the parenthesis to be one of the search terms.


I think Atom2 basically covered it, but just to reiterate....

Your grep expression basically translates to: "match any of the following characters: ()helowrd".

Since your echo string contains one or more of those characters, grep prints it as a match.
_________________
-= the D@7@k|n& =-
Back to top
View user's profile Send private message
dataking
Apprentice
Apprentice


Joined: 20 Apr 2005
Posts: 251

PostPosted: Fri Aug 01, 2014 5:50 pm    Post subject: Re: Grep expression help. Reply with quote

John R. Graham wrote:
dataking wrote:
....and '-'.
Incorrect. Inside brackets, the '-' is a range operator, not a member of the character class.

- John


My bad, you are correct, since "[a-z]" and "[0-9]" are valid character specifications, not including the "-".

I think an earlier post even covered that as the "-"'s location in the expression has relevance.

/facepalm
_________________
-= the D@7@k|n& =-
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum