How to match two or morre patterns on the same line?
Hi
Is there a way to match two or more number patterns in a number string. Eg. 01 05 17 02 08 03 05 11 15 07 09 07 20 19 05 Here the pattern of 05 and 07 is repeated twice in line 2 and 3 Is this possible to do with some linux commands. Thank you |
would be nice to show us the pattern you use, and also please tell us the language and other important information
|
Welcome to LQ!
You will need to provide a more complete definition of your task in order for others to provide useful help. Please review the Site FAQ for guidance in asking well formed questions, in particular the links at bottom of that page. Based on your original question you will need to provide a much better definition of what constitutes a "pattern", and the raw data, or strings in which those patterns are to be recognized. Also, as already noted by pan64, you will need to tells us what language or environment you are working within, and whether alternatives are acceptable. Finally, it is always good form to begin by telling what you have already tried and what problesm you have encountered. Very often, that information will provide valuable insight into the problem and your own skill level which greatly help others to provide useful replies. |
I am using bash as the interpreter
I wanted to match two or more items in the same line. For example I want 05 and 07 in bold be together on the same line. 01 05 17 02 08 03 05 11 15 07 09 07 20 19 05 I tried this Code:
egrep "05|07" nums.txt After doing more googling, I found the solution Code:
egrep "05.+07|07.+05" nums.txt Now is there a way to match 3 patterns on the same line? I googled it but I couldn't find it. Perhaps awk or something more powerful, maybe? Any ideas??? |
Quote:
Short answer - awk. |
Quote:
|
you need to check regex groups and repetition
|
The simple way would be to pipe multiple instances of grep into each other.
Another way would be to use Perl's pattern matching to take advantage of positive look-ahead assertions: (?=pattern) If your version of grep supports the -P option you might be able to do it without Perl Code:
grep -P '(?=.*?07)(?=.*?05)(?=.*20)^.*$' See 'man perlre' |
Or the much more elegant and readable
Code:
awk "/07/ && /05/ && /20/" |
Thanks Turbocapitalist and syg00. I tested both codes and they worked flawlessly. :) :) :)
|
probably:
Code:
grep -P "(05|07)(.*(05|07))+" |
Quote:
|
If you have a single line in a shell variable, say $line, (or you happen to have a while loop that reads the input line by line) then you can examine $line like this
Code:
if [[ $line == *07* -a $line == *05* -a $line == *20* ]] |
Thanks for the suggestion friend its really knowledgeable thread. :-)
|
All times are GMT -5. The time now is 03:47 PM. |