dhudnall has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I am trying to write a small program/function that will isolate a string(s) in a sentence. What I am doing is I am parsing a file that contains a list of SQLs. The SQLs ofcourse contain a list of tables from which the SQL is either (updating, deleting, inserting into etc). What I want is to just isolate the table name in the SQL. The file contains ONE SQL per line. I am going to offer some really bad sample code to demonstrate how I would like this to work. I have tried countless solutions and haven't been able to get it to work yet. Please help if you can.
#The first thing I want to do is to be able to call the function from +the command line along with the name of the file to parse followed by + the keyword to isolate. $FileName=shift; $String=shift; #Then I would open the file to parse before going any further open(INFILE, $FileName) or die "Cant open the file\n"; #Then I think I should convert the file to a list to parse @file=<INFILE>; #Then I guess I would do something like the following #While reading in from the file while(<INFILE>) { #Save the contents of EACH line $TheLine = $_; #Then I would search through each element of the line foreach $x (@file) #or $TheLine { #Then I would check to see if the string entered on the command line e +xists in the line (which it always will because I will know which SQL +s e.g. delete, update, etc...I am searching for in the file) if(exists $TheLine{$String}) { #Then I want to delete (or replace) everything BEFORE the keyword $Str +ing with a blank.....basically meaning erase it. This is where I am a +bsolutely lost at because I don't know of a relatively easy way to do + this. I figured I would use the S// operator something like s/.*$String//g #Basically do something like that #This would leave only the String I was looking for FOLLOWED by the re +st of the line. }#end if

Replies are listed 'Best First'.
Re: isolating strings
by toolic (Bishop) on Jun 28, 2007 at 15:57 UTC

    If you have an input file named sql.txt with the following contents:

    this is sql1 line1 this here is sql2 and now we have sql3 for a change

    and if you use the following code:

    #!/usr/bin/env perl use warnings; use strict; my $FileName = $ARGV[0]; my $String = $ARGV[1]; open(INFILE, $FileName) or die "Cant open the file\n"; # read in each line of the input file while (<INFILE>) { if (/$String/) { # If the $String is found... s/^.*$String//; # Delete $String and everything # before it. print; # then just print to STDOUT } }

    then the following output will be produced if you execute this program with the arguments "sql.txt sql1":

    line1

    Is this what you are trying to accomplish? If not, then please provide some sample contents of your input file, and what your output should look like. Also, it is useful if you post the exact code which you have attempted; the code you provided does not compile.

      Or to avoid matching twice:
      while (<INFILE>) { if (s/^.*$String//) { print; } }
      Toolic: that was exactly what I was looking for. Thank you very much. Sorry if I wasn't very specific. The output is exactly how I wanted it and I just simply made it write to another file. Thanks
Re: isolating strings
by ikegami (Patriarch) on Jun 28, 2007 at 15:30 UTC

    Problem #1:

    @file=<INFILE>;

    reads the entire file, so the subsequent

    while(<INFILE>)

    will exit immediately.