Sayed, Irfan (Irfan) wrote: > >> -----Original Message----- >> From: Rob Dixon [mailto:[EMAIL PROTECTED] >> Sent: Tuesday, July 18, 2006 9:09 PM >> To: beginners@perl.org >> Subject: Re: search option in perl >> >> Sayed, Irfan (Irfan) wrote: >> >>>I need to search a file for a specific string thru perl script. >>> >>>for example i want to put following condition in the script. >>> >>>--- > first search the string string1 >>>----> and if that string found then search for another string string2 >>>----> if both the string found then print both the string on shell >>>prompt >> >> >> Hi Irfan >> >> I think you want to search a file for all lines that contain both string >> 1 and string2, is that right? This does what you want >> >> > perl -n -e "print if /string1/ and /string2/" file >> >> But it may be that string1 and string2 can be on separate lines. Please >> could you calrify for us? Thanks, > > Hi rob, > you are right . > > basically I need to search two different strings in one file which are > at different lines. > means string1 is on one line and string2 is on another line > > can u plz give then actual syntax to achieve this ? that will be really > good.
Irfan. (Please put your responses at the bottom of the post you are replying to) I'm still not exactly sure, but try this. It prints all lines from the file after 'string1' and then 'string2' is found. use strict; use warnings; open my $fh, 'file' or die $!; my ($found1, $found2); while (<$fh>) { next unless $found1 +=/string1/; next unless $found2 +=/string2/; print; } I hope this helps. Rob -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>