Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

File Reading and Writing in Perl

Posted on 2006-05-10
4
Medium Priority
?
229 Views
Last Modified: 2010-03-05
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.
0
Comment
Question by:vgirijanaidu
2 Comments
 
LVL 85

Accepted Solution

by:
ozo earned 1000 total points
ID: 16653295
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
 
LVL 17

Assisted Solution

by:mjcoyne
mjcoyne earned 1000 total points
ID: 16657098
#!/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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question