searching multiple patterns in perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting searching multiple patterns in perl
# 1  
Old 04-11-2011
searching multiple patterns in perl

Hi,

I have code like:
Quote:
#!/usr/bin/perl

use strict;
use warnings;

my $path = "/home/test.txt";


open (INPUT,"$path") || die ("Could not open file $!\n");

while (<INPUT>)
{

if ($_ =~ /^\s.*Rels:/ || /^\s.*prods.pl/)
{

print $_;
}


}
Output it is comming as:

Rels: WM2
Rels: WG2
Rels: 5
- pre/prods.pl
Rels: 6
Rels: 7
Rels: 8
Rels: 10
Rels: Int

But i want only "Rels: 5" pattern Just above "- pre/prods.pl".

By creating temp file i am able to do that. But i dont want to use temp file.

Thanks..
# 2  
Old 04-11-2011
So, you want to go back from seeing line 4 to output line 3? Save the last Rel line in one variable and print it when you see the prods.pl line.
# 3  
Old 04-11-2011
Can u please tell me how to do it?
# 4  
Old 04-11-2011
how about with awk?
Code:
awk '!/-pre\/prods.pl/{x=$0}/- pre\/prods.pl/{print x}' file

# 5  
Old 04-11-2011
Actually i want it in perl. Please tell me in perl only.
# 6  
Old 04-11-2011
Split your if-two-patterns into two if-one-patterns, the rel one saves in a variable (what rel am I in), the other prints that variable.
# 7  
Old 04-11-2011
Quote:
Originally Posted by Anjan1
Actually i want it in perl. Please tell me in perl only.

Code:
perl -ane 'if($_ =~ /Rels/) {$x = $_};if ($_ =  /.*prods\.pl/){print "$x"}' file1
Rels: 5

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Searching multiple patterns and construct SQL

Hello, I have attached 2 files 1) Original_table_definition.txt => which has definition of 3 tables 2) describe_table_output.txt => which has again 3 tables definition gotten thorugh doing a show table or describe table way. Now difference between 3 tables are that tablea has no... (2 Replies)
Discussion started by: nv186000
2 Replies

2. Shell Programming and Scripting

Searching multiple patterns using awk

Hello, I have the following input file: qh1adm 20130710111201 : tp import all QH1 u6 -Dsourcesystems=BFI,EBJ qh1adm 20130711151154 : tp import all QH1 u6 -Dsourcesystems=BFI,EBJ qx1adm 20130711151154 : tp count QX1 u6 -Dsourcesystems=B17,E17,EE7 qh1adm 20130711151155 : tp import all... (7 Replies)
Discussion started by: kcboy
7 Replies

3. Shell Programming and Scripting

a column containing multiple patterns perl

If U have a question if a file is 33 ABC 276 LRR pir UJU 45 BCD 777 HIGH pred IJJ 67 BGH 66 LRR_1 prcc KIK 77 GYH 88 LOW pol KKK perl -lne '$a++ if /LRR/,/LOW/, /HIGH/; END {print $a+0}' (2 Replies)
Discussion started by: cdfd123
2 Replies

4. UNIX for Dummies Questions & Answers

Grep in Perl - Searching through multiple files

I'm attempting to use grep in Perl with very little success. What I would like to do in Perl is get the output of the following grep code: grep -l 'pattern' * This gives me a list of all the files in a directory that contain the pattern that was searched. My attempts to do this in Perl... (4 Replies)
Discussion started by: WongSifu
4 Replies

5. Shell Programming and Scripting

Searching for multiple patterns in a file

Hi All, I have a file in which i have to search for a pattern from the beginning of the file and if the pattern is found , then i have to perform a reverse search from that line to the beginning of the file to get the first occurrence of another pattern. sample input file hey what are you... (8 Replies)
Discussion started by: Kesavan
8 Replies

6. Shell Programming and Scripting

Perl, searching multiple files and printing returned line to new file

I am trying to find a way to utilise the full potential of my cpu cores and memory on my windows machine. Now, I am quite familiar with grep, however, running a Unix based OS is not an option right now. Unfortunately, the 32 bit grep for windows that I am running, I cannot run multiple... (1 Reply)
Discussion started by: Moloch
1 Replies

7. Shell Programming and Scripting

Searching for multiple patterns in files

I have a situation where I need to search for multiple strings (error messages) such as 'aborted' 'file not found' etc in directory having logs. I have put all the error messages in a text file and using the command. grep -f <textfile> <filetobegrepped> I'm doing this thru a script where I... (5 Replies)
Discussion started by: bornon2303
5 Replies

8. UNIX for Dummies Questions & Answers

Perl searching and printing multiple target in the same line

Hello, I'm trying to create a program in perl called myfind.pl; To use the program: (at the command line)$ program.pl keyword filename note: the keyword is any word or regular expression and it should display the result just like when you 'cat' the file name but with the keyword in... (2 Replies)
Discussion started by: Horizon666
2 Replies

9. 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

10. Shell Programming and Scripting

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... (10 Replies)
Discussion started by: Juha
10 Replies
Login or Register to Ask a Question