View previous topic :: View next topic |
Author |
Message |
dE_logics Advocate
Joined: 02 Jan 2009 Posts: 2253 Location: $TERM
|
Posted: Wed Jul 30, 2014 8:33 am Post subject: Grep expression help. |
|
|
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 |
|
|
mvaterlaus Apprentice
Joined: 01 Oct 2010 Posts: 234 Location: Switzerland
|
Posted: Wed Jul 30, 2014 8:48 am Post subject: |
|
|
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 |
|
|
Atom2 Apprentice
Joined: 01 Aug 2011 Posts: 185
|
Posted: Wed Jul 30, 2014 10:40 am Post subject: Re: Grep expression help. |
|
|
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. Evenwould 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 |
|
|
dE_logics Advocate
Joined: 02 Jan 2009 Posts: 2253 Location: $TERM
|
Posted: Fri Aug 01, 2014 5:24 pm Post subject: Re: Grep expression help. |
|
|
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. Evenwould 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 |
|
|
dataking Apprentice
Joined: 20 Apr 2005 Posts: 251
|
Posted: Fri Aug 01, 2014 5:38 pm Post subject: Re: Grep expression help. |
|
|
Atom2 wrote: | 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 |
|
|
John R. Graham Administrator
Joined: 08 Mar 2005 Posts: 10590 Location: Somewhere over Atlanta, Georgia
|
Posted: Fri Aug 01, 2014 5:44 pm Post subject: Re: Grep expression help. |
|
|
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 |
|
|
dataking Apprentice
Joined: 20 Apr 2005 Posts: 251
|
Posted: Fri Aug 01, 2014 5:47 pm Post subject: Re: Grep expression help. |
|
|
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 |
|
|
dataking Apprentice
Joined: 20 Apr 2005 Posts: 251
|
Posted: Fri Aug 01, 2014 5:50 pm Post subject: Re: Grep expression help. |
|
|
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 |
|
|
|