if($line = m/$string/) { print "$line\n"; } --- "Avadhani, Subramanya" <[EMAIL PROTECTED]> wrote:
> 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 > ===== Anthony Ettinger Phone: (408) 656-2473 [EMAIL PROTECTED] http://www.apwebdesign.com __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com _______________________________________________ Perl-Unix-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs