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 02-05-2014, 05: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, 05:45 AM   #2
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,367

Rep: Reputation: 2747Reputation: 2747Reputation: 2747Reputation: 2747Reputation: 2747Reputation: 2747Reputation: 2747Reputation: 2747Reputation: 2747Reputation: 2747Reputation: 2747
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, 06: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, 06:29 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,782

Rep: Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304
see man page of grep, in general grep --version should work
 
1 members found this post helpful.
Old 02-05-2014, 07:57 AM   #5
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
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 08:01 AM.
 
Old 02-05-2014, 09:56 AM   #6
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,774

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
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, 11:14 AM   #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, 12:36 PM   #8
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,774

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
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, 01: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, 01:15 PM   #10
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
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 02:07 PM
[SOLVED] grep -c not working in Linux nishanthp82 Linux - Newbie 4 01-27-2012 01:01 AM
grep not working gdanko Linux - General 11 07-15-2010 02:28 PM
grep *.tpl is not working ravipat Linux - Newbie 3 07-03-2008 08:18 PM
-r parameter not working in grep learnfast Linux - Newbie 1 03-11-2005 04:17 AM

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

All times are GMT -5. The time now is 08:23 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
Open Source Consulting | Domain Registration