Linux - NewbieThis 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
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 need a regular expression for searching a string in lines which satisfies the below conditions....
1) strings containing only digits and a single lowercase alphabet
2) digits are seperated by "," symbol
3) the string must not beging and end with the alphabet
Distribution: Solaris 9 & 10, Mac OS X, Ubuntu Server
Posts: 1,197
Rep:
Quote:
Originally Posted by sharath.bv
Hi all,
Please check if the below syntax truely satisfies the given conditions...
$_=~/^[0-9]+(\,)*[a-z][0-9]+/
You must try it and see. Create a sample file with lines of text, some of which have the pattern and some of which don't. Use your search pattern and see what it finds. Then you will see that it does not meet the conditions described in your first post.
Then try mine. I think it does, but there was some slight ambiguity in your conditions. I interpreted them to mean that commas separated all the elements.
This sounds like homework, but your posting history says maybe not....
This is the kind of thing where you have to be very careful to test all possible outcomes. For example, where can the commas be? ..between the numbers or also between the letters and numbers? Also, how many numbers are possible?
Here's one example (used with SED)
sed -r 's/[0-9],?[0-9][a-z][0-9],?[0-9]/ZZZ/g' filename (Looks for the match and replaces all instances with "ZZZ".)
Translation: match any digit, followed by optional comma, another digit, a letter, another digit, optional comma, and another digit. Add one more comma in there, or a 2-digit number and it won't match.
To match any number of digits and optional commas, things will obviously get a bit trickier. The best way to proceed is to write something simple and test it. Then keep adding to it until it behaves the way you want.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.