LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   + in grep not working (https://www.linuxquestions.org/questions/linux-newbie-8/in-grep-not-working-4175493783/)

thunderstorm1! 02-05-2014 05:35 AM

+ in grep not working
 
~ $ echo aa | grep "a*"
aa
~ $ echo aa | grep "a+"
~ $ echo aa | grep "[a-z]"
aa
~ $ echo aa | grep "[a-z][a-z]"
aa
~ $ echo aa | grep "[a-z]+"


can some one help why the + is not working .

allend 02-05-2014 05:45 AM

From 'info grep'
Quote:

In basic regular expressions the meta-characters `?', `+', `{', `|',
`(', and `)' lose their special meaning; instead use the backslashed
versions `\?', `\+', `\{', `\|', `\(', and `\)'.
Code:

bash-4.2$ echo aa | grep "a+"
bash-4.2$ echo aa | grep "a\+"
aa
bash-4.2$ echo aa | grep -E "a+"
aa


thunderstorm1! 02-05-2014 06:25 AM

Thanks a lot .

I tried them

echo aa | grep -E "a+" works
but
echo aa | grep "a\+" is still not working

my bash version is GNU bash, version 3.2.0(1)

not sure how to get the version of grep

pan64 02-05-2014 06:29 AM

see man page of grep, in general grep --version should work

jlinkels 02-05-2014 07:57 AM

From the man page:
Quote:

In GNU grep, there is no difference in available functionality between basic and extended syntaxes.
Grep version
Code:

jlinkels@donald-pc:/tmp$ grep -V
GNU grep 2.6.3
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

Now this shows that there apparently is a difference between grep and grep -E. And that is in contradiction with the manual page.
Code:

jlinkels@donald-pc:/tmp$ echo aa | grep "a+"
jlinkels@donald-pc:/tmp$ echo aa | grep -E "a+"
aa

For me "+" never worked in grep, but I thought it was my fault. Now I see that it apparently is only evaluated using the extended syntax. However, I can't find any reference to "+" being part of the extended syntax.
Escaping the + symbol is nonsense, it is part are the regular expression, not to be taken as literal character.

rknichols 02-05-2014 09:56 AM

Quote:

Originally Posted by thunderstorm1! (Post 5111960)
Thanks a lot .

I tried them

echo aa | grep -E "a+" works
but
echo aa | grep "a\+" is still not working

my bash version is GNU bash, version 3.2.0(1)

not sure how to get the version of grep

Your backslash character is being interpreted (and removed) by the shell. You need to pass a literal backslash to grep. Any of these should work:
Code:

echo aa | grep 'a\+'
echo aa | grep "a\\+"
echo aa | grep a\\+


thunderstorm1! 02-05-2014 11:14 AM

Thanks all,

grep -V
grep (GNU grep) 2.14
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.

This seems to be my grep version .

echo aa | grep 'a\+'
echo aa | grep "a\\+"
echo aa | grep a\\+

All three are working fine . Also i did not understand the part about shell removing my back slash . Please help me in understanding it . Where can i find information on how shell would react to '\' at different places .

rknichols 02-05-2014 12:36 PM

The manpage for bash has a section on quoting. Understanding of paragraphs 5 through 7 is essential for doing much with the shell command line.

thunderstorm1! 02-05-2014 01:11 PM

Thanks everyone , it really helped a lot

jlinkels 02-05-2014 01:15 PM

There is a difference between grep and egrep concerning escaping meta-characters.

Quote:

Basic vs Extended Regular Expressions
In basic regular expressions the meta-characters ?, +, {, |, (, and ) lose their special meaning; instead use the backslashed versions \?, \+, \{, \|, \(, and \).
I assume everyone ever using grep understood this, but me. :study:

jlinkels


All times are GMT -5. The time now is 03:58 AM.