LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-23-2008, 07:13 PM   #1
sharky
Member
 
Registered: Oct 2002
Posts: 569

Rep: Reputation: 84
grep is maddening


I am attempting to grep for terms that contain letters followed by a single digit. Example svls3 or svls4. I've tried 'egrep 'svls[1-9]' but it returns any 'svls' sequence with any numerical characters such as svls16 or svls59. It even returns svls60 and I don't have a 0 in my expression.

According to the docs I've read "A range of single-digit numbers can be specified as: [0-9]

Obviously I'm doing something wrong but I can't get it.
 
Old 10-23-2008, 07:26 PM   #2
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Maybe try egrep 'svls[0-9]\b'
\b is word boundary
 
Old 10-23-2008, 07:56 PM   #3
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 60
Your problem is that grep says "true" and prints the line as soon as it sees 's' 'v' 'l' 's' followed by a number between 1 and 9.

So, the regular expression engine starts at the left. It sees 's-', then 'sl-', then 'svl-', then 'svls', then 'svls1', at which point "Ding ding ding, we have a winner!"

Then it prints the line. It doesn't matter if another number follows, since you didn't tell it anything about what has to happen next. As Keefaz says, you could add \b for a word boundary or \s for whitespace or $ to specify that the item needs to be at the end of a line or...
 
Old 10-23-2008, 08:13 PM   #4
sharky
Member
 
Registered: Oct 2002
Posts: 569

Original Poster
Rep: Reputation: 84
Thanks for all the suggestions. Some docs I had suggested putting a space at the end of the expression: egrep "svls[1-9] ". This did not work for me because the target was at the end of the line. So I changed the line input which was easy since it was being piped through awk. I put the target in the middle of the line. Problem solved.

Out of curiosity I went back and tried with the \b with and the target at the end of the line. It work like a charm. The $ character did not work: egrep "svls[1-9]$" returns 'Illegal variable name.'

Thanks again for the help.
 
Old 10-23-2008, 09:49 PM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Single quotes are required to protect "$" from being interpreted by the shell instead of by GREP. So this should work:

egrep 'svls[1-9]$'
 
Old 10-24-2008, 02:50 PM   #6
jan61
Member
 
Registered: Jun 2008
Posts: 235

Rep: Reputation: 47
Moin,

a way to do it is to tell grep to look for "svls" + 1 digit, followed by a non digit or linefeed:
Code:
jan@jack:~/tmp/kv2csv> echo -e "abc svls9abc\ncxsvls10xx\ndlwsvls3\nwl svls2 ll"
abc svls9abc
cxsvls10xx
dlwsvls3
wl svls2 ll
jan@jack:~/tmp/kv2csv> echo -e "abc svls9abc\ncxsvls10xx\ndlwsvls3\nwl svls2 ll" | \
> grep -E 'svls[1-9]([^0-9]|$)'
abc svls9abc
dlwsvls3
wl svls2 ll
I used extended regex (option -E), that makes things easier.

Jan

Last edited by jan61; 10-24-2008 at 03:30 PM.
 
Old 11-01-2008, 09:39 PM   #7
ernst
Member
 
Registered: May 2006
Location: Sydney
Distribution: Debian etch, SkoleLinux
Posts: 77

Rep: Reputation: 15
My KDE menu has an application KRegExpEditor.
Just now for the first time I had some time to understand it (there is help) and have constructed some regular expressions.

I remember that in the past I have lost hours because of incorrect regular expressions (say in apache config files etc.). When there are so many parts in your task that could go wrong then it is helpful if you can isolate smaller parts.

I am confident that with KRegExpEditor I will get at last the regular expressions right the first time.
I am grateful to the developers of this program.
 
  


Reply



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
Trying to understand pipes - Can't pipe output from tail -f to grep then grep again lostjohnny Linux - Newbie 15 03-12-2009 10:31 PM
LXer: Mandriva Linux - Wonderful and Maddening LXer Syndicated Linux News 0 07-02-2008 08:40 AM
how to grep multiple filters with grep LinuxLover Linux - Enterprise 1 10-18-2007 07:12 AM
ps -ef|grep -v root|grep apache<<result maelstrombob Linux - Newbie 1 09-24-2003 11:38 AM
Maddening sendmail problem -- cant send mail to remote hosts! HELP! Bungo2000 Linux - Networking 11 06-09-2003 03:33 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:32 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