CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
[Solved] Using a Perl scrip to read a Text File and parse sinlge entries in seperate txt files

 



SteffenBaier
Novice

Aug 21, 2009, 5:18 AM

Post #1 of 16 (3833 views)
[Solved] Using a Perl scrip to read a Text File and parse sinlge entries in seperate txt files Can't Post

Hello,

I am trying to read a text file with this content (random Phone numbers):

1009
1008
1007
1006

I would like to parse each single Phone Number into a file that contains the Phone Number as part of the Name.

I created the following:

Code
  $data_file="Number.csv";   
open(DAT, $data_file) || die("Could not open file!");
@raw_data=<DAT>;
close(DAT);

foreach $phonenumber (@raw_data)
{
print "LINE1 = $phonenumber";
print "LINE1_PROXY = 1\n";
print "LINE1_CALLID = Sip Phone $phonenumber\n";

$filename="sip_.cfg";
open OUT, ">$filename";

print OUT "LINE1 = $phonenumber";
print OUT "LINE1_PROXY = 1\n";
print OUT "LINE1_CALLID = Sip Phone $phonenumber\n";
close OUT;
}




Above "prints" this on the screen but only creates one File with the last Entry (1006) as I am not aware if I can use the $phonenumber as part of the name....:

LINE1 = 1009
LINE1_PROXY = 1
LINE1_CALLID = Sip Phone 1009

LINE1 = 1008
LINE1_PROXY = 1
LINE1_CALLID = Sip Phone 1008

LINE1 = 1007
LINE1_PROXY = 1
LINE1_CALLID = Sip Phone 1007

LINE1 = 1006
LINE1_PROXY = 1
LINE1_CALLID = Sip Phone 1006


How can I modify the Perl Script in order to use sip_$phonenumber.cfg so I get 4 seperate Files with the seperate content ..?

THX

Steff


(This post was edited by SteffenBaier on Aug 25, 2009, 6:15 AM)


KevinR
Veteran


Aug 21, 2009, 9:10 AM

Post #2 of 16 (3830 views)
Re: [SteffenBaier] [HELP] Using a Perl scrip to read a Text File and parse sinlge entries in seperate txt files [In reply to] Can't Post

You use the same file name over and over in the loop:

$filename="sip_.cfg";

So there will only be one file with data in it from the last iteration of the loop. And you never include the phone numbers as part of the filename.
-------------------------------------------------


SteffenBaier
Novice

Aug 24, 2009, 12:45 AM

Post #3 of 16 (3822 views)
Re: [KevinR] [HELP] Using a Perl scrip to read a Text File and parse sinlge entries in seperate txt files [In reply to] Can't Post

Hi,

the Number.csv contains this:

1009
1008
1007
1006


My Question is how to change the above script so it modifies the output Filename into:

sip_1009.cfg
sip_1008.cfg
sip_1007.cfg
sip_1006.cfg

???

How would I create this ..? Can I use a variable in the Filename like:


Code
$filename="sip_$phonenumber.cfg";



spider
User


Aug 24, 2009, 1:34 AM

Post #4 of 16 (3820 views)
Re: [SteffenBaier] [HELP] Using a Perl scrip to read a Text File and parse sinlge entries in seperate txt files [In reply to] Can't Post

The $filename="sip_$phonenumber.cfg"; should work.
Why don't you just test your own idea?


SteffenBaier
Novice

Aug 24, 2009, 1:51 AM

Post #5 of 16 (3819 views)
Re: [spider] [HELP] Using a Perl scrip to read a Text File and parse sinlge entries in seperate txt files [In reply to] Can't Post

I had already done that but it does not work. No file is created...


spider
User


Aug 24, 2009, 2:01 AM

Post #6 of 16 (3817 views)
Re: [SteffenBaier] [HELP] Using a Perl scrip to read a Text File and parse sinlge entries in seperate txt files [In reply to] Can't Post

Well, it should work.
I also tested it.
Copied you code excactly, except the input file.
Tested it first and worked as you described with just one out-file.
Then I changed to adding the phonenumber inside the filename, and then it outputted one file foreach phonenumber.


Code
my(@raw_data) = (1001,1002,1003,1004,1005) ; 
foreach $phonenumber (@raw_data) {
print "LINE1 = $phonenumber";
print "LINE1_PROXY = 1\n";
print "LINE1_CALLID = Sip Phone $phonenumber\n";
$filename="sip_$phonenumber.cfg";
open OUT, ">$filename";
print OUT "LINE1 = $phonenumber";
print OUT "LINE1_PROXY = 1\n";
print OUT "LINE1_CALLID = Sip Phone $phonenumber\n";
close OUT;
}



spider
User


Aug 24, 2009, 2:04 AM

Post #7 of 16 (3816 views)
Re: [spider] [HELP] Using a Perl scrip to read a Text File and parse sinlge entries in seperate txt files [In reply to] Can't Post

By the way, I also recomend to improve the code to use strict:


Code
#!/usr/bin/perl 
use strict ;
use warnings ;

my(@raw_data) = (1001,1002,1003,1004,1005) ;
foreach my $phonenumber (@raw_data) {
print "LINE1 = $phonenumber";
print "LINE1_PROXY = 1\n";
print "LINE1_CALLID = Sip Phone $phonenumber\n";
my $filename="sip_$phonenumber.cfg";
open OUT, ">$filename";
print OUT "LINE1 = $phonenumber";
print OUT "LINE1_PROXY = 1\n";
print OUT "LINE1_CALLID = Sip Phone $phonenumber\n";
close OUT;
}



SteffenBaier
Novice

Aug 24, 2009, 2:15 AM

Post #8 of 16 (3815 views)
Re: [spider] [HELP] Using a Perl scrip to read a Text File and parse sinlge entries in seperate txt files [In reply to] Can't Post

Hi,

that works great in case I add the @raw_data Information into the actual Perl Script.
In real Life I would love to read that Info from a txt file as it will be around 390+ Numbers ...

If I change your Example and add the Part were I read the txt File I get this error:


Quote
C:\Temp>test1.pl
Global symbol "$data_file" requires explicit package name at C:\Temp\test1.pl li
ne 7.
Global symbol "$data_file" requires explicit package name at C:\Temp\test1.pl li
ne 8.
Global symbol "@raw_data" requires explicit package name at C:\Temp\test1.pl lin
e 9.
Global symbol "@raw_data" requires explicit package name at C:\Temp\test1.pl lin
e 13.
Execution of C:\Temp\test1.pl aborted due to compilation errors.





This is the modified script:


Code
#!/usr/bin/perl  
use strict ;
use warnings ;

#my(@raw_data) = (1001,1002,1003,1004,1005) ;

$data_file="Number.csv";
open(DAT, $data_file) || die("Could not open file!");
@raw_data=<DAT>;
close(DAT);


foreach my $phonenumber (@raw_data) {
print "LINE1 = $phonenumber\n";
print "LINE1_PROXY = 1\n";
print "LINE1_CALLID = Sip Phone $phonenumber\n";
my $filename="sip_$phonenumber.cfg";
open OUT, ">$filename";
print OUT "LINE1 = $phonenumber\n";
print OUT "LINE1_PROXY = 1\n";
print OUT "LINE1_CALLID = Sip Phone $phonenumber\n";
close OUT;
}



Any Ideas how to resolve above and be able to read the raw_data from a txt file ..?

THX

Steff


spider
User


Aug 24, 2009, 2:34 AM

Post #9 of 16 (3813 views)
Re: [SteffenBaier] [HELP] Using a Perl scrip to read a Text File and parse sinlge entries in seperate txt files [In reply to] Can't Post

Of cource you can still read the input from file, it was just easier for me to add the testdata into the script in sted of adding it into an extra file.

The reason for this error is since you added the use strict, it forces you to write better code.
You have to declear all variables before you use it.
All you have to do is to add my infront of the $data_file="Number.csv" ; and in front of the @raw_data=<DAT> ;

You could also consider to use while in sted of reading all data into a file. If the input file is big I it is better to use the while.

Ex:

Code
#!/usr/bin/perl 
use strict ;
use warnings ;

my $data_file="Number.csv";
open(DAT, $data_file) || die("Could not open file!");

while(<DAT>) {
my($phonenumber) ;
#Added this test, in sted of reading the complet line.
#This gives the possibillityes to add comments to the file and it ignores whitespace at the end of the line ex.
if($_ =~ /^(\d+)/) {
$phonenumber = $1 ;
}
print "LINE1 = $phonenumber\n";
print "LINE1_PROXY = 1\n";
print "LINE1_CALLID = Sip Phone $phonenumber\n";
my $filename="sip_$phonenumber.cfg";
open OUT, ">$filename";
print OUT "LINE1 = $phonenumber\n";
print OUT "LINE1_PROXY = 1\n";
print OUT "LINE1_CALLID = Sip Phone $phonenumber\n";
close OUT;
}

my(@raw_data) = (1001,1002,1003,1004,1005) ;
foreach my $phonenumber (@raw_data) {
print "LINE1 = $phonenumber";
print "LINE1_PROXY = 1\n";
print "LINE1_CALLID = Sip Phone $phonenumber\n";
my $filename="sip_$phonenumber.cfg";
open OUT, ">$filename";
print OUT "LINE1 = $phonenumber";
print OUT "LINE1_PROXY = 1\n";
print OUT "LINE1_CALLID = Sip Phone $phonenumber\n";
close OUT;
}
close(DAT);



SteffenBaier
Novice

Aug 24, 2009, 3:04 AM

Post #10 of 16 (3810 views)
Re: [spider] [HELP] Using a Perl scrip to read a Text File and parse sinlge entries in seperate txt files [In reply to] Can't Post

Hello Spider,

that works perfect ;-)

THX for your Help

Steff


SteffenBaier
Novice

Aug 25, 2009, 1:25 AM

Post #11 of 16 (3801 views)
Re: [SteffenBaier] [HELP] Using a Perl scrip to read a Text File and parse sinlge entries in seperate txt files [In reply to] Can't Post

Hi,

as this works now perfect could I also Import something like this:

the Number.csv contains this:

1009,A Password
1008,A Password
1007,A Password
1006,A Password

Therefore the Perl Script would need to check:

Value A, Value B


Code
  open OUT, ">$filename";  
print OUT "LINE1 = $phonenumber\n";
print OUT "Password = $password\n";
print OUT "LINE1_PROXY = 1\n";
print OUT "LINE1_CALLID = Sip Phone $phonenumber\n";


Any quick Hints?

THX

Steff


(This post was edited by SteffenBaier on Aug 25, 2009, 1:27 AM)


spider
User


Aug 25, 2009, 6:09 AM

Post #12 of 16 (3794 views)
Re: [SteffenBaier] [HELP] Using a Perl scrip to read a Text File and parse sinlge entries in seperate txt files [In reply to] Can't Post

By the way, I see now that I forgot to delete the foreach from my code when changing to the while, but I guess you understood that.
If you input file is a csv file, that the easy and best way is to use a split. Then you get all parameteres into an array. Also when you add more parameters.

Ex:


Code
#!/usr/bin/perl 
use strict ;
use warnings ;

my $data_file="Number.csv";
open(DAT, $data_file) || die("Could not open file!");

while(<DAT>) {
my(@line) = split(',',$_) ;

print "LINE1 = $line[0]\n";
print "LINE1_PROXY = 1\n";
print "LINE1_CALLID = Sip Phone $line[0]\n";
print "USER = $line[1]\n" ;
my $filename="sip_$line[0].cfg";
open OUT, ">$filename";
print OUT "LINE1 = $line[0]\n";
print OUT "LINE1_PROXY = 1\n";
print OUT "LINE1_CALLID = Sip Phone $line[0]\n";
print OUT "USER = $line[1]\n" ;
close OUT;
}

close(DAT);



SteffenBaier
Novice

Aug 25, 2009, 6:15 AM

Post #13 of 16 (3793 views)
Re: [spider] [HELP] Using a Perl scrip to read a Text File and parse sinlge entries in seperate txt files [In reply to] Can't Post

Top Man !!



Excellent stuff and a lot learned!



Thank you

Steffen


FishMonger
Veteran / Moderator

Aug 25, 2009, 7:39 AM

Post #14 of 16 (3789 views)
Re: [spider] [HELP] Using a Perl scrip to read a Text File and parse sinlge entries in seperate txt files [In reply to] Can't Post

Since the exact same output is going to multiple handles, I'd use IO::Tee.

Since the print function takes a list, I'd use a single print statement and I'd also use descriptive names for the vars.


Code
#!/usr/bin/perl 

use strict;
use warnings;
use IO::Tee;

my $data_file = "Number.csv";
open my $CSV, '<', $data_file or die "Could not open $data_file $!";

while( <$CSV> ) {
chomp;
my($phone, $password) = split(/,/, $_);
my $filename = "sip_$phone.cfg";

open my $sip_cfg, '>', $filename or die "Could not open $filename $!";
my $tee = IO::Tee->new(\*STDOUT, $sip_cfg);

print $tee "LINE1 = $phone\n",
"LINE1_PROXY = 1\n",
"LINE1_CALLID = Sip Phone $phone\n",
"USER = $password\n";
}



spider
User


Aug 25, 2009, 9:33 AM

Post #15 of 16 (3783 views)
Re: [FishMonger] [HELP] Using a Perl scrip to read a Text File and parse sinlge entries in seperate txt files [In reply to] Can't Post

Agree upon the more desciptive names, and the single print statement.

The tee I have never used. What does it do different?


FishMonger
Veteran / Moderator

Aug 25, 2009, 10:50 AM

Post #16 of 16 (3779 views)
Re: [spider] [HELP] Using a Perl scrip to read a Text File and parse sinlge entries in seperate txt files [In reply to] Can't Post

tee is a standard unix utility that reads from stdin and multipexes the output to stdout and 1 or more files.

Perl has several tee modules.

Tee - Pure Perl emulation of GNU tee http://search.cpan.org/~dagolden/Tee-0.13/lib/Tee.pod

File::Tee - replicate data sent to a Perl stream - http://search.cpan.org/~salva/File-Tee-0.05/lib/File/Tee.pm

PerlIO::tee - Multiplex output layer - http://search.cpan.org/~gfuji/PerlIO-Util-0.70/lib/PerlIO/tee.pm

IO::Tee - Multiplex output to multiple output handles - http://search.cpan.org/~kenshan/IO-Tee-0.64/Tee.pm

And probably a few others.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives