File Reading and Writing in Perl

I know this must be a very simple perl script, but since I am so new to Perl, could someone please provide me with this script.

I need a script that

Takes in a file F.html

1) reads the contents of a file into a string A
2) replaces all the occurences of an apostrophe (')  in A by 2 apostrophes
3) initialize another string B to contain "SELECT * FROM TABLE WHERE TEXT = '"
4) concatenate A to B into another variable called C
5) concatenate "')\nGO" to this C
6) insert C into a new file with the name F.sql


Could someone please help me out with this?

Thanks sooo much.
vgirijanaiduAsked:
Who is Participating?
 
ozoCommented:
open F,"<F.html" or die "can't open F.html $!";
#1)
my $A = join'',<F>;
close F;
#2)
$A =~ s/'/''/g;
#3)
my $B = "SELECT * FROM TABLE WHERE TEXT = '";
#4)
my $C = $B.$A;
#5)
$C .= "')\nGO";
#6)
open F,">F.sql" or die die "can't open F.sql $!";
print F $C;
close F;

0
 
mjcoyneCommented:
#!/usr/bin/perl -w
use strict;

my $stringB = "SELECT * FROM TABLE WHERE TEXT = '";

open(FH, "org_file.txt") or die "Can't open org_file.txt: $!\n";
my $stringA = do {local($/); <FH>};

$stringA =~ s/'/"/g;
my $stringC = $stringB . $stringA . "')\nGO";

open (NEW, ">F.sql") or die "Can't open F.sql: $!\n";
print NEW $stringC;

I'm assuming you need all these things in discrete steps and variables from the way you asked the question.  You could, of course, shorten it up a bit:

#!/usr/bin/perl -w
use strict;

open(FH, "org_file.txt") or die "Can't open org_file.txt: $!\n";
open (NEW, ">F.sql") or die "Can't open F.sql: $!\n";

my $stringA = do {local($/); <FH>};
$stringA =~ s/'/"/g;
print NEW "SELECT * FROM TABLE WHERE TEXT = '" . $stringA . "')\nGO";
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.