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 07-29-2015, 07:55 PM   #1
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 3,535

Rep: Reputation: 51
echo 8888|grep 8+ does not match.


Code:
bill@server:~$ echo 8888|grep 88*
8888
$
But
Code:
bill@server:~$ echo 8888|grep 8+ 
bill@server:~$
However, the second command line is, AFAIK, equivalent to the first. What am I missing here?

Last edited by stf92; 07-29-2015 at 07:59 PM.
 
Old 07-29-2015, 08:10 PM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_10{.0|.1|.2}
Posts: 4,268
Blog Entries: 1

Rep: Reputation: 2303Reputation: 2303Reputation: 2303Reputation: 2303Reputation: 2303Reputation: 2303Reputation: 2303Reputation: 2303Reputation: 2303Reputation: 2303Reputation: 2303
No they are not the same.

Code:
echo 8888|grep 88*
Matches 88 plus anything or nothing following as interpreted by the shell (* is effectively nullified by shell). When properly escaped it would mean 88 followed by zero or more 8s.

Code:
echo 8888|grep 8+
Is intended to match an 8 followed by one or more 8's... except it doesn't!

It does match this...

Code:
echo 8888+ |grep 8+
It will work more as expected if you properly quote and/or escape the expression to hide it from the shell AND tell grep to interpret it as a repetition modifier...

Code:
echo 8888|grep '8\+'
8888

OR

echo 8888|grep 8\\+
8888

Last edited by astrogeek; 07-29-2015 at 08:34 PM. Reason: Completeness and unquoted case
 
1 members found this post helpful.
Old 07-30-2015, 03:36 PM   #3
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 3,535

Original Poster
Rep: Reputation: 51
According to the manual
Quote:
Basic vs Extended Regular Expressions
In basic regular expressions the meta-characters ?, +, {, |, (, and ) lose their special meaning; instead use the backslashed versions \?, \+, \{, \|, \(, and \).
the plus character must be escaped in order to retain its special meaning. As to the asterix, who is not listed, it has the usual meaning in reg exps without it being escaped. Which is what you explained. And always quote the whole expression to avoid the shell interference. Thanks.

By the way, then one difference between basic and extended reg-exps, is that in extended ones we have the additional special characters listed above. However,
Code:
$ echo 8888 | grep -E '8+'
$
Maybe the reason is in
Quote:
In GNU grep, there is no difference in
available functionality between basic and extended syntaxes. In other
implementations, basic regular expressions are less powerful. The
following description applies to extended regular expressions;
differences for basic regular expressions are summarized afterwards.
which I find obscure (hard to understand).
 
1 members found this post helpful.
Old 07-30-2015, 03:54 PM   #4
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_10{.0|.1|.2}
Posts: 4,268
Blog Entries: 1

Rep: Reputation: 2303Reputation: 2303Reputation: 2303Reputation: 2303Reputation: 2303Reputation: 2303Reputation: 2303Reputation: 2303Reputation: 2303Reputation: 2303Reputation: 2303
Yea, that does look a little obscure.

I am not a regex expert, but I use them daily and can usually work out what is needed even in what I consider complex cases - usually.

I have learned to always quote and escape in the shell and escape in sed scripts - saves much trouble.

Glad that helped!
 
  


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
[SOLVED] script to match patterns on files and echo on the same line rebelbuttmunch Linux - Newbie 3 11-22-2011 05:46 AM
grep | xargs -I echo $(foo; bar; echo $(fee; fi; fo; fum)) == questionable output.. GrapefruiTgirl Programming 11 12-07-2010 07:02 PM
Grep with Echo Myiagros Linux - General 3 04-26-2010 10:12 AM
grep/sed/awk - find match, then match on next line gctaylor1 Programming 3 07-11-2007 08:55 AM
grep output on stdout and grep output to file don't match xnomad Linux - General 3 01-13-2007 04:56 AM


All times are GMT -5. The time now is 06:27 PM.

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