ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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...
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.'
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.