LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to match two or morre patterns on the same line? (https://www.linuxquestions.org/questions/programming-9/how-to-match-two-or-morre-patterns-on-the-same-line-4175621638/)

L4Z3R 01-15-2018 12:36 AM

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

pan64 01-15-2018 01:07 AM

would be nice to show us the pattern you use, and also please tell us the language and other important information

astrogeek 01-15-2018 01:40 AM

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.

L4Z3R 01-15-2018 04:39 AM

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


01 05 17 02 08
03 05 11 15 07
09 07 20 19 05

Line 2 and 3 is what I want. Line 1 only gives me 05 only. I don't want 05 Or 07 in the line, I need both.

After doing more googling, I found the solution

Code:

egrep "05.+07|07.+05" nums.txt

03 05 11 15 07
09 07 20 19 05

This solves the two matched patterns on the same line.

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???

syg00 01-15-2018 04:53 AM

Quote:

Originally Posted by L4Z3R (Post 5806609)
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.

Which is not what you asked initially - you asked for a count. Define your requirements properly.

Short answer - awk.

L4Z3R 01-15-2018 05:13 AM

Quote:

Originally Posted by syg00 (Post 5806615)
Which is not what you asked initially - you asked for a count. Define your requirements properly.

Short answer - awk.

My bad. I meant match. I changed the title and the first post to be more clearer. Thanks

pan64 01-15-2018 05:22 AM

you need to check regex groups and repetition

Turbocapitalist 01-15-2018 05:23 AM

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)^.*$'
That will match any line with 05, 07, and 20 all present at least once in any order.

See 'man perlre'

syg00 01-15-2018 05:42 AM

Or the much more elegant and readable
Code:

awk "/07/ && /05/ && /20/"
perlre ain't the be-all and end-all ...

L4Z3R 01-15-2018 08:03 AM

Thanks Turbocapitalist and syg00. I tested both codes and they worked flawlessly. :) :) :)

pan64 01-15-2018 08:28 AM

probably:
Code:

grep -P "(05|07)(.*(05|07))+"
you can add 20 too (or something else) if required easily.

L4Z3R 01-15-2018 07:28 PM

Quote:

Originally Posted by pan64 (Post 5806689)
probably:
Code:

grep -P "(05|07)(.*(05|07))+"
you can add 20 too (or something else) if required easily.

Thanks pan64. :thumbsup:

MadeInGermany 01-16-2018 11:31 AM

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* ]]
then
  printf "%s\n" "$line"
fi


Laurence_Burke 01-19-2018 05:40 AM

Thanks for the suggestion friend its really knowledgeable thread. :-)


All times are GMT -5. The time now is 03:47 PM.