you could slurp the whoel file into one string and then search that. my $text;
{ local( $/ ) ; # Makes the entire file contents be read in one go, by changing what perl recognises as the line break char..... see docs.. open (NEWFILE,"<$file") or die "Cannot open file '$file'"; $text=<NEWFILE>; close (NEWFILE); } if ($text =~/$match/g) { } etc...... Marty --- "Friedel.Wittrock" <[EMAIL PROTECTED]> wrote: > Hi, > > try this: > > -------------------------------------------------------- > #!/usr/bin/perl > > print "Enter the Input file name.: "; $file = > <STDIN>; > print "Enter String to search....: "; $str = > <STDIN>; > print "Enter the Output file name: "; $outfile = > <STDIN>; > > chomp($outfile); chomp($str); chomp($file); > > die "can't open $outfile\n" unless open(OUT, > ">>$outfile"); > die "can't open $file\n" unless open(IN, > "<$file"); > while (<IN>) { > chomp; > if (/$str/) { > print "$_\n"; > print OUT "$_\n"; > } > } > close(IN); > close(OUT); > -------------------------------------------------------- > > regards > Friedel > > > -----Ursprüngliche Nachricht----- > Von: > [EMAIL PROTECTED] > [mailto:[EMAIL PROTECTED] > Auftrag von > Avadhani, Subramanya > Gesendet: Montag, 30. August 2004 13:44 > An: [EMAIL PROTECTED] > Betreff: [Perl-unix-users] Searching the entire > string > > > Hi list, > I have written a small script in Perl to take input > filename, > string to search and outfilename. This script > currently searches the > string in a file and writes the same string to the > outfile(if found)also > display the same on console. I want to change the > functionality to work > something like grep command in Unix. I want the > entire line which is > surrounding the string instead of just the sting, > how do I do it ..? > I am pasting the script below, which will search > for a string > say Subramanya and prints just the input string from > a file test.txt > say. Please also advise on whether I can make it > more powerful to search > from multiple files etc. > > > #!/usr/local/bin/perl > ####### Accept Input file name > print ("Enter the Input file name : "); > $file = <STDIN>; > ####### Accept the expression to search > print ("Enter String to search :"); > $str = <STDIN>; > ####### Accept Output file name > print ("Enter the Output file name : "); > $outfile = <STDIN>; > chomp ($outfile); > chomp ($str); > chomp ($file); > > #### File open operation fails if file doesn't exist > (Exception) > > unless (open(MYFILE, $file)) > { > die("Cannot Open Input file\n"); > } > $line = <MYFILE>; > while ($line ne "") > { > while ($line =~ /$str/g) > { > $match = $&; > print ("$match\n"); > open (outfile,">>$outfile"); > print outfile ("$match\n"); > } > $line = <MYFILE>; > } > > close (MYFILE); > close (outfile); > > ####### Please remove the outfile after use. As this > would eatup disk > space. ########### > > Test file ########################## > Hi I am Subramanya > I live in juigj > iorth > ojr > ;[k > hl > Subramanya > iqerioj > 1234Subramanya > Subramanya Avadhani > iijriogj > ioj > [pjtr > hk > Avadhani Subramanya > iojioqer > \opjth > Subramanya > I am Subramanya > I am here Subramanya > ############################ > Here I want the entire line and not just the string > entered by user, > till EOL(say Subramanya). > > Thanks, > Subbu > > _______________________________________________ > Perl-Unix-Users mailing list > [EMAIL PROTECTED] > To unsubscribe: > http://listserv.ActiveState.com/mailman/mysubs > --- > Incoming mail is certified Virus Free. > Checked by AVG anti-virus system > (http://www.grisoft.com). > Version: 6.0.744 / Virus Database: 496 - Release > Date: 24.08.2004 > > --- > Outgoing mail is certified Virus Free. > Checked by AVG anti-virus system > (http://www.grisoft.com). > Version: 6.0.744 / Virus Database: 496 - Release > Date: 24.08.2004 > > _______________________________________________ > Perl-Unix-Users mailing list > [EMAIL PROTECTED] > To unsubscribe: > http://listserv.ActiveState.com/mailman/mysubs > ___________________________________________________________ALL-NEW Yahoo! Messenger - all new features - even more fun! http://uk.messenger.yahoo.com _______________________________________________ Perl-Unix-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs