Perl: Match a line with multiple search patterns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl: Match a line with multiple search patterns
# 1  
Old 04-07-2008
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  
Old 04-07-2008
Code:
awk '/123/ && /abd/ && /QWERTY/{print}'  file

# 3  
Old 04-08-2008
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  
Old 04-08-2008
Thanks danmero and KevinADC! Smilie I took the example from KevinADC as I'm trying to get on top of perl scripting Smilie
# 5  
Old 04-08-2008
Quote:
Originally Posted by KevinADC
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
....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  
Old 04-08-2008
Quote:
Originally Posted by danmero
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  
Old 04-08-2008
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..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk pattern match by looping through search patterns

Hi I am using Solaris 5.10 & ksh Wanted to loop through a pattern file by reading it and passing it to the awk to match that value present in column 1 of rawdata.txt , if so print column 1 & 2 in to Avlblpatterns.txt. Using the following code but it seems some mistakes and it is running for... (2 Replies)
Discussion started by: ananan
2 Replies

2. Shell Programming and Scripting

Search Multiple patterns and display

Hi, I have scenario like below and need to search for multiple patterns Eg: Test Time Started= secs Time Ended = secc Green test Test Time Started= secs Time Ended = secc Green test Output: I need to display the text starting with Test and starting with Time... (2 Replies)
Discussion started by: weknowd
2 Replies

3. Shell Programming and Scripting

Match multiple patterns sequentially in order - grep or awk

Hello. grep v2.21 Debian 8 I wish to search for and output these patterns in order; "From " "To: " "Subject: " "Message-Id: " "Date: " "To: " grep works, but not in strict order... $ grep -a -E "^From |^Subject:|^From: |^Message-Id: |^Date: |^To: " InboxResult; From - Wed Feb 18... (10 Replies)
Discussion started by: DSommers
10 Replies

4. Shell Programming and Scripting

Search patterns in multiple logs parallelly.

Hi All, I am starting a service which will redirect its out put into 2 logs say A and B. Now for succesful startup of the service i need to search pattern1 in log A and pattern2 in log B which are writen continuosly. Now my requirement is to find the patterns in the increasing logs A and B... (19 Replies)
Discussion started by: Girish19
19 Replies

5. Shell Programming and Scripting

How to search Multiple patterns in unix

Hi, I tried to search multiple pattern using awk trans=1234 reason=LN MISMATCH rec=`awk '/$trans/ && /'"$reason"'/' file` whenevr i tried to run on command promt it is executing but when i tried to implment same logic in shell script,it is failing i.e $rec is empty ... (6 Replies)
Discussion started by: ns64110
6 Replies

6. Shell Programming and Scripting

search multiple patterns

I have two lists in a file that look like a b b a e f c d f e d c I would like a final list a b c d e f I've tried multiple grep and awk but can't get it to work (8 Replies)
Discussion started by: godzilla07
8 Replies

7. Shell Programming and Scripting

Match multiple patterns in a file and then print their respective next line

Dear all, I need to search multiple patterns and then I need to print their respective next lines. For an example, in the below table, I will look for 3 different patterns : 1) # ATC_Codes: 2) # Generic_Name: 3) # Drug_Target_1_Gene_Name: #BEGIN_DRUGCARD DB00001 # AHFS_Codes:... (3 Replies)
Discussion started by: AshwaniSharma09
3 Replies

8. Shell Programming and Scripting

Search multiple patterns in multiple files

Hi, I have to write one script that has to search a list of numbers in certain zipped files. For eg. one file file1.txt contains the numbers. File1.txt contains 5,00,000 numbers and I have to search each number in zipped files(The number of zipped files are around 1000 each file is 5 MB) I have... (10 Replies)
Discussion started by: vsachan
10 Replies

9. Shell Programming and Scripting

print lines which match multiple patterns

Hi, I have a text file as follows: 11:38:11.054 run1_rdseq avg_2-5 999988.0000 1024.0000 11:50:52.053 run3_rdrand 999988.0000 1135.0 128.0417 11:53:18.050 run4_wrrand avg_2-5 999988.0000 8180.5833 11:55:42.051 run4_wrrand avg_2-5 999988.0000 213.8333 11:55:06.053... (2 Replies)
Discussion started by: annazpereira
2 Replies

10. Shell Programming and Scripting

Perl - How to search a text file with multiple patterns?

Good day, great gurus, I'm new to Perl, and programming in general. I'm trying to retrieve a column of data from my text file which spans a non-specific number of lines. So I did a regexp that will pick out the columns. However,my pattern would vary. I tried using a foreach loop unsuccessfully.... (2 Replies)
Discussion started by: Sp3ck
2 Replies
Login or Register to Ask a Question