The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
AWK: Multiple patterns per line Plavixo UNIX for Dummies Questions & Answers 1 05-05-2008 04:31 PM
Multiple line match using sed SiftinDotCom Shell Programming and Scripting 15 03-28-2008 02:12 PM
read and match multiple lines in perl zx1106 Shell Programming and Scripting 5 03-14-2008 10:21 PM
Multiple file processing - 1st line only to process - search/match substring toneatlondon Shell Programming and Scripting 2 01-02-2008 09:20 PM
How to parameterize multiple search patterns and generate a new file augustinep UNIX for Dummies Questions & Answers 6 07-30-2003 08:50 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-07-2008
Juha Juha is offline
Registered User
  
 

Join Date: Sep 2006
Posts: 36
Perl: Match a line with multiple search patterns

Hi

I'm not very good with the serach patterns and I'd need a sample how to find a line that has multiple patterns.

Say I want to find a line that has "abd", "123" and "QWERTY" and there can be any characters or numbers between the serach patterns, I have a file that has thousands of lines and each of them can contain one or two of above patterns.

What I want is to print out a line that has all those 3 search patterns.

Thanks,
//Juha
  #2 (permalink)  
Old 04-07-2008
danmero danmero is offline Forum Advisor  
  
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 1,420
Code:
awk '/123/ && /abd/ && /QWERTY/{print}'  file
  #3 (permalink)  
Old 04-08-2008
KevinADC KevinADC is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2008
Posts: 731
It is not clear if you need to check for all the patterns or any of the patterns:

All of them:

Code:
if (/123/ && /abd/ && /QWERTY/) {
    found all patterns
}

any of them:

Code:
if (/123/ ||  /abd/ || /QWERTY/) {
    found one or more patterns
}
  #4 (permalink)  
Old 04-08-2008
Juha Juha is offline
Registered User
  
 

Join Date: Sep 2006
Posts: 36
Thanks danmero and KevinADC! I took the example from KevinADC as I'm trying to get on top of perl scripting
  #5 (permalink)  
Old 04-08-2008
danmero danmero is offline Forum Advisor  
  
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 1,420
Quote:
Originally Posted by KevinADC View Post
It is not clear if you need to check for all the patterns or any of the patterns:
Look like the OP ask for all patterns on the same line
Quote:
Originally Posted by Juha View Post
....I want to find a line that has "abd", "123" and "QWERTY" and there can be any characters or numbers between the serach patterns...
  #6 (permalink)  
Old 04-08-2008
KevinADC KevinADC is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2008
Posts: 731
Quote:
Originally Posted by danmero View Post
Look like the OP ask for all patterns on the same line
Yes, but the next sentence makes that ambiguous (to me anyway):

Quote:
I have a file that has thousands of lines and each of them can contain one or two of above patterns.
  #7 (permalink)  
Old 04-08-2008
Juha Juha is offline
Registered User
  
 

Join Date: Sep 2006
Posts: 36
ok.. Let me clarify a little:

Originally I wanted to print a line only if ALL the patterns can be found from that line, so this was ok:

Code:
if (/123/ && /abd/ && /QWERTY/) {
    found all patterns
}
Now, I found a different scenario, which I've tried to figure out... but have to admit that could not..

I'd like to print the line only if I can find "123" and abc", but not "efg":

I thought maybe I could use:
Code:
if (/123/ && /abc/ !& /efg/) {
    found all patterns
}
But !& does not seem to be a valid operator... What would be a way around it?

Thanks.. and sorry for being a bit unclear..
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:07 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0