Perl text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl text file
# 1  
Old 10-31-2003
Perl text file

I've got a csv file called "passlist.txt" which looks like:

firstname1,lastname1,checkin1,numberofbags1,destination1
firstname2,lastname2,checkin2,numberofbags2,destination2
firstname3,lastname3,checkin3,numberofbags3,destination3
" " " " "
etc...

I've written a perl script to populate a MySQL table with each row's information:

#!/usr/bin/perl -w
# insert_data_from_file.plx

use strict;
use DBI;

my ($dbh, $sth, $firstname, $lastname, $checkin, $numberofbags, $destination, $rows);

$dbh=DBI->connect('dbi:mysql:test','root','elephant')
|| die "Error opening database: $DBI::errstr\n";

$sth=$dbh->prepare
("INSERT INTO checkin (firstname, lastname, checkin, numberofbags, destination)
VALUES (?, ?, ?, ?, ? )")
|| die "Error inserting record: $DBI::errstr\n";

$rows=0;

while (<>)
{
chomp;
($firstname, $lastname, $checkin, $numberofbags, $destination) = split(/,/);
$sth->execute($firstname, $lastname, $checkin, $numberofbags, $destination)
|| die "Couldn't insert record: $DBI::errstr";

$rows+=$sth->rows();
}

print "$rows new rows have been added to checkin";

$dbh->disconnect || die "Failed to disconnect\n";

#-#-#-#-#

I execute the file with "perl filename.plx passlist.txt" and I'm receiving the following error message:

DBD::mysql::st execute failed: Column 'firstname' cannot be null at insert_data_from_file.plx line 23, <> line 7.
Couldn't insert record: Column 'firstname' cannot be null at insert_data_from_file.plx line 23, <> line 7.

Could it be that my txt file is in the wrong format? Anyone got any ideas? I'm quite new to perl so any help is appreciated. Thanks in advance...
# 2  
Old 10-31-2003
With your program, I think you have to execute by

cat passlist.txt | perl -w insert_data_from_file.plx

Otherwise, you have to add additional code to accept a command-line parameter and open() the file yourself.
# 3  
Old 10-31-2003
hmm, I get the same error message with that command... could it be something to do with the format of the text file?
# 4  
Old 10-31-2003
just a stab in teh dark (i havent done any perl in months)

but your while (<>) statement is only accepting input from the command line.. it is not opening a file handle to get its data.

so in actuality you are feeding your program 1 paramiters out of 5 and you dont have a delimiter in there that your script is looking for. i got 20 that says your prog might work if you type this.

so i would open a filehandler in your program then feed that to the while loop.

also

i have never used split the way you are but i dont think you are passing any data to it. try this.

($firstname, $lastname, $checkin, $numberofbags, $destination) = split(/,/),$_;

you may also want to try a differant field seperator. for giggles.

as a test type the following on a command line:

$(name of your script) Optimus/Prime/10:00/5/Chicago

since you have the /usr/bin/perl in the script you dont need to call perl on the command line either just make the script +x permisions.

Last edited by Optimus_P; 10-31-2003 at 11:13 AM..
# 5  
Old 10-31-2003
take a look at a DBI script i wrote when i was playing with DBI

Code:
#!/usr/bin/perl
#
# OBJECTIVE:
# 1) Get the OMS # from the 944.
# 2) Get the SKU/LOT/ACTUAL QUANITY from the DB
# 3) insert those 3 things into the 944
#

use DBI;

$FILE='Q206.944';
$COUNT=0;

open (FILE944, "$FILE") or die "Can not read file ($!)";
open (FILEOUT, ">outme") or die "Can not write file ($!)";

while (<FILE944>) {
        chomp;  # remove end of line
        if ($_ =~ /^03/) {
                $doc_code=substr($_,39,9); # grab the doc_code number from the 03 segment

                #OMS PROD
                my $dbh = DBI->connect( 'dbi:Informix:standard@prod', 'USER', 'PASS', {
                        PrintError=>1,
                        RaiseError=>1 });

                $sth = $dbh->prepare("
                        SELECT prod_code, lot_code, actual_qty
                        FROM wattrn
                        WHERE doc_code = ? ");  # get all of my needed information from the DB
                $sth->execute($doc_code);

                while ( $array_ref = $sth->fetchrow_arrayref ) {        #feed the return results to an array
                        push @jackpot, [ @$array_ref ];
                }
                $dbh->disconnect();
        print FILEOUT "$_\n";   #print out the current line to the new file.

        }
        elsif ($_ =~ /^30/ ) {
                $prod=pack A20, $jackpot[$COUNT][0];    #set the proper space needed for the prod_code
                $lot =pack A30, $jackpot[$COUNT][1];    #set the proper space needed for the log_code
                substr($_,10,50)="${prod}${lot}";       #insert the DB info into the file in the 30 segment
                print FILEOUT "$_\n";
        }
        elsif ($_ =~ /^33/ ) {
                $pad="         $jackpot[$COUNT][2].00"; #makeup for provias mistakes
                $aqty=pack A12, substr($pad,-12,12) ;   #packit up for the proper space for actual_qty
                substr($_,92,12)="${aqty}";
                print FILEOUT "$_\n";
        $COUNT++;       #incroment the counter to step up to the next set of values for the 30 and 33 segments.
        }
        else {
        print FILEOUT "$_\n";
        }

}

# 6  
Old 10-31-2003
Thanks for the replies mate... the funny thing I've just noticed is that even though I'm getting this error message when I execute the script, the data is actually being inserted into the table perfectly! :P Can't work out what the error message is trying to tell me if all seems to be well... since the script's gonna be running on cron I might just turn a blind eye to the error message, after all, it's not like I'm going to see it! :/ If anyone can work out why I might be getting the message I would like to know, however it doesn't seem to be critical since the script is doing what it's meant to be doing.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with creating a text file in perl with file creation date.

Hi, I am quite new to Perl scripting and i need to create a .TXT file using perl, with fields (A,B,C,D,E), and this text file should be named with current file creation date "XYZ_CCYYMMDD.TXT" (i.e.XYZ_2011042514:33 PM). Can anyone who has done this, please share their expertise on this... (5 Replies)
Discussion started by: msrahman
5 Replies

2. Shell Programming and Scripting

shell or perl script needed for ldif file to text file conversion

This is the ldf file dn: sdcsmsisdn=1000000049,sdcsDatabase=subscriberCache,dc=example,dc=com objectClass: sdcsSubscriber objectClass: top postalCode: 29600 sdcsServiceLevel: 10 sdcsCustomerType: 14 givenName: Adelia sdcsBlackListAll: FALSE sdcsOwnerType: T-Mobile sn: Actionteam... (1 Reply)
Discussion started by: LinuxFriend
1 Replies

3. UNIX for Advanced & Expert Users

perl text file processing using hash

Hi Experts, I have this requirement to process large files (200MB+).Format of files is like: recordstart val1 1 val2 2 val3 4 recordstart val1 5 val2 6 val3 1 val4 1 recordstart val1 ... (4 Replies)
Discussion started by: mtomar
4 Replies

4. UNIX for Dummies Questions & Answers

SOLVED: Text file compare using perl

I have two text file.... One text file contain in this format...... keyvalue.txt \SUM\SUM_cam.c \SUM\SUM_cam.h \SUM\SUM_command.c \SUM\SUM_command.h \SUM\SUM_dab.c \SUM\SUM_dmb.c \SUM\SUM_eventHandler.h \SUM\SUM_eventHandler_dab.c \SUM\SUM_eventHandler_dmb.c ... (6 Replies)
Discussion started by: suvenduperl
6 Replies

5. Shell Programming and Scripting

Perl Split for text in file

Hi all I have written Perl script to swap the strings in the second a third column from a text file. My input file format is : the|empty|the|det lake|empty|lake|conj_and was|empty|was|auxpass drained|empty|drained|conj_and birds|empty|bird|s|nn The expected output file format is... (11 Replies)
Discussion started by: my_Perl
11 Replies

6. Shell Programming and Scripting

Perl - Enter text in a file in a place.

Hi, I have a simple question: I need to enter some text in a text file at a certain place via perl. I would first need to find that specific text in the file and then I would like to insert a line after that particular line. Say I have this text file: I am a great Perl Programmer I... (1 Reply)
Discussion started by: som.nitk
1 Replies

7. Shell Programming and Scripting

Convert XML file into TEXT file using PERL seript

Dear Yogesh..."Convert XML file into TEXT file using PERL seript"... can u help me regarding this issue...plz Thanks Rudro (0 Replies)
Discussion started by: Rudro
0 Replies

8. Shell Programming and Scripting

Perl Sort on Text File

Hi, I have a file of names and I want perl to do a sort on this file. How can I sort this list of names using perl? I'm thinking of a command like: @sorted = sort { lc($a) cmp lc($b) } @not_sorted # alphabetical sort The only thing I'm sort of unsure of is, how would I get the name in my... (6 Replies)
Discussion started by: eltinator
6 Replies

9. Shell Programming and Scripting

text file formatting by perl

have a simple text file as input.i have to print that file in paragraph format.whenevr it finds "\n" in the input text it should start printing in next paragraph in output file.also a fixed amount of space should be given before start writing in every paragraph. the input and output file format... (5 Replies)
Discussion started by: avik1983
5 Replies

10. Shell Programming and Scripting

PERL:print 32 variables into a text file

okay, down below is the script. i have 32 words put into 32 variables and i want perl to print all those 32 variables into one text document, each word under another in the text file. the text files called times.txt Sorry about the length of the script print " VToshiba ... (2 Replies)
Discussion started by: perleo
2 Replies
Login or Register to Ask a Question