LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-15-2018, 12:36 AM   #1
L4Z3R
Member
 
Registered: Jan 2018
Posts: 34

Rep: Reputation: Disabled
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

Last edited by L4Z3R; 01-15-2018 at 05:11 AM.
 
Old 01-15-2018, 01:07 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,894

Rep: Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317
would be nice to show us the pattern you use, and also please tell us the language and other important information
 
Old 01-15-2018, 01:40 AM   #3
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,269
Blog Entries: 24

Rep: Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195Reputation: 4195
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.
 
Old 01-15-2018, 04:39 AM   #4
L4Z3R
Member
 
Registered: Jan 2018
Posts: 34

Original Poster
Rep: Reputation: Disabled
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???
 
Old 01-15-2018, 04:53 AM   #5
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,130

Rep: Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121
Quote:
Originally Posted by L4Z3R View Post
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.
 
Old 01-15-2018, 05:13 AM   #6
L4Z3R
Member
 
Registered: Jan 2018
Posts: 34

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by syg00 View Post
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
 
Old 01-15-2018, 05:22 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,894

Rep: Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317
you need to check regex groups and repetition
 
1 members found this post helpful.
Old 01-15-2018, 05:23 AM   #8
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,320
Blog Entries: 3

Rep: Reputation: 3725Reputation: 3725Reputation: 3725Reputation: 3725Reputation: 3725Reputation: 3725Reputation: 3725Reputation: 3725Reputation: 3725Reputation: 3725Reputation: 3725
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'
 
1 members found this post helpful.
Old 01-15-2018, 05:42 AM   #9
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,130

Rep: Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121
Or the much more elegant and readable
Code:
awk "/07/ && /05/ && /20/"
perlre ain't the be-all and end-all ...
 
2 members found this post helpful.
Old 01-15-2018, 08:03 AM   #10
L4Z3R
Member
 
Registered: Jan 2018
Posts: 34

Original Poster
Rep: Reputation: Disabled
Thanks Turbocapitalist and syg00. I tested both codes and they worked flawlessly.
 
Old 01-15-2018, 08:28 AM   #11
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,894

Rep: Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317
probably:
Code:
grep -P "(05|07)(.*(05|07))+"
you can add 20 too (or something else) if required easily.
 
1 members found this post helpful.
Old 01-15-2018, 07:28 PM   #12
L4Z3R
Member
 
Registered: Jan 2018
Posts: 34

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
probably:
Code:
grep -P "(05|07)(.*(05|07))+"
you can add 20 too (or something else) if required easily.
Thanks pan64.
 
Old 01-16-2018, 11:31 AM   #13
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,804

Rep: Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203
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
 
Old 01-19-2018, 05:40 AM   #14
Laurence_Burke
LQ Newbie
 
Registered: Jan 2018
Posts: 5

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


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to count keyword enclosed between pattern not evaluating correctly liketheshell Programming 4 05-09-2016 03:14 PM
how to recognise pattern in numbers vostrushka Linux - Software 2 01-18-2014 05:12 PM
multiple pattern search and count the no. of occurances raghu123 Programming 4 06-03-2009 04:50 PM
Script Help: How to count a matching pattern in one line? dv502 Programming 3 12-13-2008 01:53 PM
Pattern count in a file --> scripting froglinux Programming 7 06-17-2006 01:55 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:44 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration