L
LHradowy
Right now I have a perl script that takes a comma separated file and adds a
couple of things to it as will as takes away the data at the end.
I have done this the hard way, by saving a file in excel and a comma
separated file, then ftp it over, dos2ux file >file1.
And this is the outcome BEFORE I run my perl script.
3xxxx18,00 0 02 00,TELN NOT
3xxxx22,00 0 03 11,CUST HAS >
Then after all that I run my perl script against it prompts user for input,
adds some data, then greps file for certain things, and creates 3 files.
What I want to do is elinate the first part of saving it as a comma
separated file. I belive I can do this in perl, but I can not split on
spaces since I have spaces that I need to be part of a column. So, (how to
explain) instead of the above mention where there is a comma, I need to
split this file, based on criteria, and also add a comma between the
columns, so it looks like above...
This is the file I get before I save it as a comma separated file.
3xxxx33 00 0 00 21 CUSTOMER HAS
3xxxx28 00 0 02 00 TELN NOT BILL
yada...
I want to avoid this step, how do I change my perl script to reflect this
instead of a comma.
Remember in the 2 and third fields there are spaces that I need.
OUTCOME
3xxxx33,BUILDING1,ROOM2,00 0 00 21,CUSTOMER HAS > 1
3xxxx66,BUILDING1,ROOM2,00 0 01 07,CUSTOMER HAS > 1
3xxxx75,BUILDING1,ROOM2,00 0 02 09,CUSTOMER HAS > 1
3xxxx85,BUILDING1,ROOM2,00 0 12 09,TELN NOT BILL
SCRIPT
*****************************
#!/opt/perl/bin/perl
use strict;
use warnings;
system ("clear"); #Clear the screen
my $acode = "204";
print "Enter BLD: ";
chomp (my $bld =<STDIN>);
my $CAPbld = uc($bld);
my $bld4=substr $CAPbld,0,4; #Pull first 4 char out of BLD for naming of
file
print "Enter Room: ";
chomp (my $room = <STDIN>);
my $CAProom = uc($room);
open my $fc, ">$bld4.cust_has" or die "$bld4.cust_has: $!";
open my $ft, ">$bld4.teln_not" or die "$bld4.teln_not: $!";
open my $fo, ">$bld4.PRTDIST.err" or die "$bld4.PRTDIST.err: $!";
while (<>) {
chomp; # Will remove the leading , or new line
my @a = split /,/, $_, -1;
my $f = /TELN/ ? $ft : /CUST/? $fc : $fo;
print $f join "," => $acode.$a[0],$CAPbld, $CAProom, $a[1], $a[2], "\n";
}
close $fc;
close $ft;
close $fo;
## Modify the cust_has file and pull only the first column.
my $fc_name = "$bld4.cust_has";
open (my $fc, $fc_name) or die "$fc_name:$!";
open my $fcC, ">$bld4.cust_has.tn" or die "$bld4.cust_has.tn: $!";
while (<$fc>) {
chomp;
my ( $FirstField,@Rest)=split /,/;
print $fcC join (",","'$FirstField',",)."\n";
}
close fc;
close fcC;
## Modify the teln_not file to take off last column
## File is now ready for report making.
my $fc_name2 = "$bld4.teln_not";
open (my $fc, $fc_name2) or die "$fc_name2:$!";
open my $fcT, ">$bld4.teln_not-1" or die "$bld4.teln_not-1: $!";
while (<$fc>) {
chomp;
my ( $FirstField1,$SecondField1,$ThirdField1,$FourthField1,@Rest)=split /,/;
print $fcT join
(",","$FirstField1","$SecondField1","$ThirdField1","$FourthField1",)."\n";
}
close fc;
close fcT;
`mv $bld4.teln_not-1 $bld4.teln_not`;
couple of things to it as will as takes away the data at the end.
I have done this the hard way, by saving a file in excel and a comma
separated file, then ftp it over, dos2ux file >file1.
And this is the outcome BEFORE I run my perl script.
3xxxx18,00 0 02 00,TELN NOT
3xxxx22,00 0 03 11,CUST HAS >
Then after all that I run my perl script against it prompts user for input,
adds some data, then greps file for certain things, and creates 3 files.
What I want to do is elinate the first part of saving it as a comma
separated file. I belive I can do this in perl, but I can not split on
spaces since I have spaces that I need to be part of a column. So, (how to
explain) instead of the above mention where there is a comma, I need to
split this file, based on criteria, and also add a comma between the
columns, so it looks like above...
This is the file I get before I save it as a comma separated file.
3xxxx33 00 0 00 21 CUSTOMER HAS
3xxxx63 00 0 01 07 CUSTOMER HAS
3xxxx75 00 0 02 09 CUSTOMER HAS
3xxxx85 00 0 12 09 TELN NOT BILL
3xxxx28 00 0 02 00 TELN NOT BILL
yada...
I want to avoid this step, how do I change my perl script to reflect this
instead of a comma.
Remember in the 2 and third fields there are spaces that I need.
OUTCOME
3xxxx33,BUILDING1,ROOM2,00 0 00 21,CUSTOMER HAS > 1
3xxxx66,BUILDING1,ROOM2,00 0 01 07,CUSTOMER HAS > 1
3xxxx75,BUILDING1,ROOM2,00 0 02 09,CUSTOMER HAS > 1
3xxxx85,BUILDING1,ROOM2,00 0 12 09,TELN NOT BILL
SCRIPT
*****************************
#!/opt/perl/bin/perl
use strict;
use warnings;
system ("clear"); #Clear the screen
my $acode = "204";
print "Enter BLD: ";
chomp (my $bld =<STDIN>);
my $CAPbld = uc($bld);
my $bld4=substr $CAPbld,0,4; #Pull first 4 char out of BLD for naming of
file
print "Enter Room: ";
chomp (my $room = <STDIN>);
my $CAProom = uc($room);
open my $fc, ">$bld4.cust_has" or die "$bld4.cust_has: $!";
open my $ft, ">$bld4.teln_not" or die "$bld4.teln_not: $!";
open my $fo, ">$bld4.PRTDIST.err" or die "$bld4.PRTDIST.err: $!";
while (<>) {
chomp; # Will remove the leading , or new line
my @a = split /,/, $_, -1;
my $f = /TELN/ ? $ft : /CUST/? $fc : $fo;
print $f join "," => $acode.$a[0],$CAPbld, $CAProom, $a[1], $a[2], "\n";
}
close $fc;
close $ft;
close $fo;
## Modify the cust_has file and pull only the first column.
my $fc_name = "$bld4.cust_has";
open (my $fc, $fc_name) or die "$fc_name:$!";
open my $fcC, ">$bld4.cust_has.tn" or die "$bld4.cust_has.tn: $!";
while (<$fc>) {
chomp;
my ( $FirstField,@Rest)=split /,/;
print $fcC join (",","'$FirstField',",)."\n";
}
close fc;
close fcC;
## Modify the teln_not file to take off last column
## File is now ready for report making.
my $fc_name2 = "$bld4.teln_not";
open (my $fc, $fc_name2) or die "$fc_name2:$!";
open my $fcT, ">$bld4.teln_not-1" or die "$bld4.teln_not-1: $!";
while (<$fc>) {
chomp;
my ( $FirstField1,$SecondField1,$ThirdField1,$FourthField1,@Rest)=split /,/;
print $fcT join
(",","$FirstField1","$SecondField1","$ThirdField1","$FourthField1",)."\n";
}
close fc;
close fcT;
`mv $bld4.teln_not-1 $bld4.teln_not`;