LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 02-05-2014, 06:35 AM   #1
thunderstorm1!
LQ Newbie
 
Registered: Oct 2013
Posts: 12

Rep: Reputation: Disabled
+ 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 .
 
Old 02-05-2014, 06:45 AM   #2
allend
Senior Member
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 4,429

Rep: Reputation: 1348Reputation: 1348Reputation: 1348Reputation: 1348Reputation: 1348Reputation: 1348Reputation: 1348Reputation: 1348Reputation: 1348Reputation: 1348
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
 
Old 02-05-2014, 07:25 AM   #3
thunderstorm1!
LQ Newbie
 
Registered: Oct 2013
Posts: 12

Original Poster
Rep: Reputation: Disabled
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
 
Old 02-05-2014, 07:29 AM   #4
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,104

Rep: Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267
see man page of grep, in general grep --version should work
 
1 members found this post helpful.
Old 02-05-2014, 08:57 AM   #5
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Wheezy/Jessie/Sid, Linux Mint DE
Posts: 4,493

Rep: Reputation: 635Reputation: 635Reputation: 635Reputation: 635Reputation: 635Reputation: 635
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.

Last edited by jlinkels; 02-05-2014 at 09:01 AM.
 
Old 02-05-2014, 10:56 AM   #6
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 2,959

Rep: Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268
Quote:
Originally Posted by thunderstorm1! View Post
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\\+
 
Old 02-05-2014, 12:14 PM   #7
thunderstorm1!
LQ Newbie
 
Registered: Oct 2013
Posts: 12

Original Poster
Rep: Reputation: Disabled
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 .
 
Old 02-05-2014, 01:36 PM   #8
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 2,959

Rep: Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268
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.
 
Old 02-05-2014, 02:11 PM   #9
thunderstorm1!
LQ Newbie
 
Registered: Oct 2013
Posts: 12

Original Poster
Rep: Reputation: Disabled
Thanks everyone , it really helped a lot
 
Old 02-05-2014, 02:15 PM   #10
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Wheezy/Jessie/Sid, Linux Mint DE
Posts: 4,493

Rep: Reputation: 635Reputation: 635Reputation: 635Reputation: 635Reputation: 635Reputation: 635
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.

jlinkels
 
  


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
Creating an alias in ksh that uses grep and includes 'grep -v grep' doug248 Linux - Newbie 2 08-05-2012 03:07 PM
[SOLVED] grep -c not working in Linux nishanthp82 Linux - Newbie 4 01-27-2012 02:01 AM
grep not working gdanko Linux - General 11 07-15-2010 03:28 PM
grep *.tpl is not working ravipat Linux - Newbie 3 07-03-2008 09:18 PM
-r parameter not working in grep learnfast Linux - Newbie 1 03-11-2005 05:17 AM


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

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration