File Reading and Writing in Perl

Posted on 2006-05-10
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.
Question by:vgirijanaidu
    LVL 84

    Accepted Solution

    open F,"<F.html" or die "can't open F.html $!";
    my $A = join'',<F>;
    close F;
    $A =~ s/'/''/g;
    my $C = $B.$A;
    $C .= "')\nGO";
    open F,">F.sql" or die die "can't open F.sql $!";
    print F $C;
    close F;

    LVL 17

    Assisted Solution

    #!/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";

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
    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…
    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…
    This video discusses moving either the default database or any database to a new volume.

    779 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

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now