Operating System - HP-UX
1820475 Members
3051 Online
109624 Solutions
New Discussion юеВ

Re: split on space instead of comma in perl

 
SOLVED
Go to solution
Ratzie
Super Advisor

split on space instead of comma in perl

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 after I have saved it in execel as a comma deliminated file, then ftp'd and dos2ux it.
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, it prompts user for input, adds some data, then greps file for certain things, and creates 3 files.

What I want to do is eliminate the first part of saving it as a comma separated file. I believe 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 > 1
3xxxx63 00 0 01 07 CUSTOMER HAS > 1
3xxxx75 00 0 02 09 CUSTOMER HAS > 1
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 =);
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 = );
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`;





2 REPLIES 2
Hein van den Heuvel
Honored Contributor
Solution

Re: split on space instead of comma in perl



You still have some more regulare expression reading to do huh?

Here is one of many possible ways to 'parse' that input date with a regexp:

perl -ne 'if (/^(\w+)\s([\d\s]+)\s(.*)$/) { $p1=$1; $p2=$2; $p3=$3; print "p1=$p1, p2=$p2, p3=$p3\n" }'

In words...

Start at the beginning of a line: ^
[actually redundant]
Remember a series of 'word characters' in $1: (\w+)
[could also use (\S+) for 'non-whitespace']
look for whitespace : \s
Remember $2 as a series of decimal digits or whitespace: ([\d\s]+)
[could use exact pattern: (\d+s\d\s\d+\s\d+)
look for whitespace : \s
Remember everything else as $3: (.*)
[could also use $' for 'everything after match]
Anchor to the end of line: $
[optional]


As an alternative try this is a perl script:

if (/\s([\d\s]+)\s/) {
chomp;
$p1=$`;
$p2=$1;
$p3=$';
print "p1=$p1, p2=$p2, p3=$p3\n"
}

Up to you to merge it into your full script!

Hein.


Ratzie
Super Advisor

Re: split on space instead of comma in perl

THanks