- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- split on space instead of comma in perl
Operating System - HP-UX
1820553
Members
2830
Online
109626
Solutions
Forums
Categories
Company
Local Language
юдл
back
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
юдл
back
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Blogs
Information
Community
Resources
Community Language
Language
Forums
Blogs
Go to solution
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-03-2004 04:18 PM
тАО08-03-2004 04:18 PM
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`;
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`;
Solved! Go to Solution.
2 REPLIES 2
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО08-03-2004 05:22 PM
тАО08-03-2004 05:22 PM
SolutionYou 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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-23-2005 01:10 AM
тАО02-23-2005 01:10 AM
Re: split on space instead of comma in perl
THanks
The opinions expressed above are the personal opinions of the authors, not of Hewlett Packard Enterprise. By using this site, you accept the Terms of Use and Rules of Participation.
Company
Learn About
News and Events
Support
© Copyright 2025 Hewlett Packard Enterprise Development LP