Results 1 to 7 of 7
Hi All,
I am trying to write a shell script that uses a regular expression to match one or more '[a-z]{3}=[0-9]' in a comma separated list which does not end ...
- 05-12-2009 #1Just Joined!
- Join Date
- Jul 2006
- Posts
- 6
Regex - Matching Comma Separated List.
Hi All,
I am trying to write a shell script that uses a regular expression to match one or more '[a-z]{3}=[0-9]' in a comma separated list which does not end in a comma:
MATCHES:
abc=1
ajk=2,wer=3
bnd=7,djd=9,jkl=4
...etc
NON-MATCHES
abc=1,
dfs=2,ath=3,
ABC=10,
Abc=3
ddc 3
agh=2 , dfd=2
abc
adg3
ab
...etc
Any help will be much appreciated.
- 05-12-2009 #2Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
For a task like this, I will usually write a quick C or Java program to do it. This is basically a parsing job, and bash isn't a great language for parsers.
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 05-13-2009 #3Linux User
- Join Date
- Aug 2006
- Posts
- 458
@op, why is agh=2 , dfd=2 not a match
why would you want to spend time coding C/Java and compiling it when there are tools like awk , Perl , Python or even bash's regular expression can do the job?
Code:awk -F"," '!/,$/{ for(i=1;i<=NF;i++){ if ( $0~/[a-z][a-z][a-z]=[0-9]/) { print $0 break } } }' file
- 05-13-2009 #4Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
Matter of personal preference I expect. Some jobs call for awk, sed, grep/egrep and all those other great Unix tools. This one was simple enough that I could write a parser in C or C++ just about as quickly. Others would prefer your solution. So, whatever works.
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 05-13-2009 #5Linux User
- Join Date
- Aug 2006
- Posts
- 458
- 05-13-2009 #6Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
Awk is a general-purpose tool - it handles lots of stuff at the cost of complexity and (possibly) performance. I'm talking about a narrow-purpose tool that does only one thing, but very efficiently. Again, my point is that this is a decision for the user and what tools they are most comfortable with. For some of these regex pattern matching problems, getting something like awk to do just what you want can take a lot more time than just doing it oneself. Not necessarily - each situation has to be evaluated on its own (de)merits.
And speaking of regex evaluators, I have designed and implemented several, and have ported (and debugged) Henry Spencer's regex library (used in nedit) to a number of platforms. I have also designed and implemented numerous parsers over the years as well as rule-processing languages that can handle extremely complex pattern matching over very large domains (used in manufacturing optimization problems). So, yes I am comfortable with low-level languages such as C, and I recognize that the original poster of this thread might not be. I don't disagree with you, but I am just pointing out that there are a number of valid approaches to solving this problem.Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 05-13-2009 #7Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
And FWIW, I have a great deal of respect for all three of A(ho), W(einberg), and K(ernighan). They are all brilliant lights in the Computer Science field and have each inspired me in my career.
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!


Reply With Quote
