Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > space deliminated to comma delinated with varried and need spaces between some columns

Reply
Thread Tools

space deliminated to comma delinated with varried and need spaces between some columns

 
 
LHradowy
Guest
Posts: n/a
 
      09-20-2004
I have file that looks like this...
1555002 00 0 04 27 TELN NOT BILL
3555007 00 0 06 00 CUSTOMER HAS
> 1

5555410 00 0 12 10 CUSTOMER HAS
> 1

6755012 00 0 12 06 CUSTOMER HAS
> 1


Notice the white spaces at beginning of the line, I DONT WANT THEM THERE
Notice the white spaces in the 2nd and 3rd columns, I NEED THEM THERE...

I need to created a perl script that takes this file and makes it look like
this
1555002,00 0 04 27,TELN NOT BILL
3555007,00 0 06 00,CUSTOMER HAS > 1
5555410,00 0 12 10,CUSTOMER HAS > 1
6755012,00 0 12 06,CUSTOMER HAS > 1

This output needs to be written to a file.
I have no idea how to start, if I split on a space " " the it will spit the
third an fourth column up. The fourth column can basically be left alone.

Thanks for the help.




 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      09-20-2004
LHradowy wrote:
> I have file that looks like this...
> 1555002 00 0 04 27 TELN
> NOT BILL 3555007 00 0 06 00
> CUSTOMER HAS
>> 1

> 5555410 00 0 12 10
> CUSTOMER HAS
>> 1

> 6755012 00 0 12 06
> CUSTOMER HAS
>> 1

>
> Notice the white spaces at beginning of the line, I DONT WANT THEM
> THERE


Please see the thread "
Replacing spaces" that was discussed here over the weekend.

> Notice the white spaces in the 2nd and 3rd columns, I NEED THEM
> THERE...


The solutions posted in the thread mentioned above will leave those alone.


> I need to created a perl script that takes this file


perldoc -f open
perldoc perlop (and check for <>)

> and makes it look like this
> 1555002,00 0 04 27,TELN NOT BILL
> 3555007,00 0 06 00,CUSTOMER HAS > 1
> 5555410,00 0 12 10,CUSTOMER HAS > 1
> 6755012,00 0 12 06,CUSTOMER HAS > 1
>
> This output needs to be written to a file.


perldoc -f open
perldoc -f print

> I have no idea how to start, if I split on a space " " the it will
> spit the third an fourth column up. The fourth column can basically
> be left alone.


So, what is the distinguishing difference between the separator for the
items in the third column on the one hand and the separator between the
third column and the fourth column on the other hand?

jue


 
Reply With Quote
 
 
 
 
Shawn Corey
Guest
Posts: n/a
 
      09-20-2004
Hi,

If the data is in fixed columns, you can use substr.

perldoc -f substr

--- Shawn

LHradowy wrote:
> I have file that looks like this...
> 1555002 00 0 04 27 TELN NOT BILL
> 3555007 00 0 06 00 CUSTOMER HAS
>
>>1

>
> 5555410 00 0 12 10 CUSTOMER HAS
>
>>1

>
> 6755012 00 0 12 06 CUSTOMER HAS
>
>>1

>
>
> Notice the white spaces at beginning of the line, I DONT WANT THEM THERE
> Notice the white spaces in the 2nd and 3rd columns, I NEED THEM THERE...
>
> I need to created a perl script that takes this file and makes it look like
> this
> 1555002,00 0 04 27,TELN NOT BILL
> 3555007,00 0 06 00,CUSTOMER HAS > 1
> 5555410,00 0 12 10,CUSTOMER HAS > 1
> 6755012,00 0 12 06,CUSTOMER HAS > 1
>
> This output needs to be written to a file.
> I have no idea how to start, if I split on a space " " the it will spit the
> third an fourth column up. The fourth column can basically be left alone.
>
> Thanks for the help.
>
>
>
>


 
Reply With Quote
 
Ian Wilson
Guest
Posts: n/a
 
      09-20-2004
LHradowy wrote:
> I have file that looks like this...
> 1555002 00 0 04 27 TELN NOT BILL
> 3555007 00 0 06 00 CUSTOMER HAS
>
>>1

>
> 5555410 00 0 12 10 CUSTOMER HAS
>
>>1

>
> 6755012 00 0 12 06 CUSTOMER HAS
>
>>1

>
>
> Notice the white spaces at beginning of the line, I DONT WANT THEM THERE
> Notice the white spaces in the 2nd and 3rd columns, I NEED THEM THERE...
>
> I need to created a perl script that takes this file and makes it look like
> this
> 1555002,00 0 04 27,TELN NOT BILL
> 3555007,00 0 06 00,CUSTOMER HAS > 1
> 5555410,00 0 12 10,CUSTOMER HAS > 1
> 6755012,00 0 12 06,CUSTOMER HAS > 1
>
> This output needs to be written to a file.
> I have no idea how to start, if I split on a space " " the it will spit the
> third an fourth column up. The fourth column can basically be left alone.
>
> Thanks for the help.
>
>


If the data always has multiple spaces (ASCII 32) between fields, I'd
try stripping the leading spaces and then converting >1 consecutive
spaces to commas:

perl -e -p 's/^ +//; s/ +/,/g' oldfile > newfile

But I expect Shawn's substr solution to be more robust. Using unpack may
be another useful approach.
 
Reply With Quote
 
Tore Aursand
Guest
Posts: n/a
 
      09-20-2004
On Mon, 20 Sep 2004 14:13:07 -0500, LHradowy wrote:
> I have file that looks like this...
> 1555002 00 0 04 27 TELN NOT BILL
> 3555007 00 0 06 00 CUSTOMER HAS
>> 1

> 5555410 00 0 12 10 CUSTOMER HAS
>> 1

> 6755012 00 0 12 06 CUSTOMER HAS
>> 1

>
> Notice the white spaces at beginning of the line, I DONT WANT THEM THERE
> Notice the white spaces in the 2nd and 3rd columns, I NEED THEM THERE...
>
> I need to created a perl script that takes this file and makes it look like
> this
> 1555002,00 0 04 27,TELN NOT BILL
> 3555007,00 0 06 00,CUSTOMER HAS > 1
> 5555410,00 0 12 10,CUSTOMER HAS > 1
> 6755012,00 0 12 06,CUSTOMER HAS > 1


If we skip everything that has got to do with the file(s), here's a
suggestion (untested);

while ( <DATA> ) {
chomp; # Get rid of line breaks
s,^\s+,,; # Remove leading spaces
my @cols = split( /\s+{2,}/, $_ ); # Split on two (or more) spaces
print join( ',', @cols ) . "\n";
}


--
Tore Aursand <(E-Mail Removed)>
"Daring ideas are like chessmen moved forward; they may be beaten, but
they may start a winning game." (Johann Wolfgang von Goethe)
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      09-20-2004
Tore Aursand wrote:
> If we skip everything that has got to do with the file(s), here's a
> suggestion (untested);
>
> while ( <DATA> ) {
> chomp; # Get rid of line breaks
> s,^\s+,,; # Remove leading spaces
> my @cols = split( /\s+{2,}/, $_ ); # Split on two (or more) spaces

-----------------------------^^^^^

Maybe you should have tested it...

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
LHradowy
Guest
Posts: n/a
 
      09-21-2004

"Tore Aursand" <(E-Mail Removed)> wrote in message
news(E-Mail Removed)...
> On Mon, 20 Sep 2004 14:13:07 -0500, LHradowy wrote:
> > I have file that looks like this...
> > 1555002 00 0 04 27 TELN NOT

BILL
> > 3555007 00 0 06 00 CUSTOMER

HAS
> >> 1

> > 5555410 00 0 12 10 CUSTOMER

HAS
> >> 1

> > 6755012 00 0 12 06 CUSTOMER

HAS
> >> 1

> >
> > Notice the white spaces at beginning of the line, I DONT WANT THEM THERE
> > Notice the white spaces in the 2nd and 3rd columns, I NEED THEM THERE...
> >
> > I need to created a perl script that takes this file and makes it look

like
> > this
> > 1555002,00 0 04 27,TELN NOT BILL
> > 3555007,00 0 06 00,CUSTOMER HAS > 1
> > 5555410,00 0 12 10,CUSTOMER HAS > 1
> > 6755012,00 0 12 06,CUSTOMER HAS > 1

>
> If we skip everything that has got to do with the file(s), here's a
> suggestion (untested);
>
> while ( <DATA> ) {
> chomp; # Get rid of line breaks
> s,^\s+,,; # Remove leading spaces
> my @cols = split( /\s+{2,}/, $_ ); # Split on two (or more) spaces
> print join( ',', @cols ) . "\n";
> }



Ahhh, I think I am forgetting something, THIS is exactly what I want!
But I am getting an error when I run it, and my skills at perl are weak.
#!/opt/perl/bin/perl

use strict;
use warnings;


while (<>) {
chomp; # Will remove the leading , or new line
s,^\s+,,; #Remove leading spaces
my @cols=split(/\s+{2,}/,$_); #Split on two (or more) spaces
print join (',',@cols)."\n";
}

user@server$ ./test.pl file
Nested quantifiers in regex; marked by <-- HERE in m/\s+{ <-- HERE 2,}/ at
../test.pl line 10.


 
Reply With Quote
 
LHradowy
Guest
Posts: n/a
 
      09-21-2004

"Ian Wilson" <(E-Mail Removed)> wrote in message
news:cinis9$fvn$(E-Mail Removed)...
> LHradowy wrote:
> > I have file that looks like this...
> > 1555002 00 0 04 27 TELN NOT

BILL
> > 3555007 00 0 06 00 CUSTOMER

HAS
> >
> >>1

> >
> > 5555410 00 0 12 10 CUSTOMER

HAS
> >
> >>1

> >
> > 6755012 00 0 12 06 CUSTOMER

HAS
> >
> >>1

> >
> >
> > Notice the white spaces at beginning of the line, I DONT WANT THEM THERE
> > Notice the white spaces in the 2nd and 3rd columns, I NEED THEM THERE...
> >
> > I need to created a perl script that takes this file and makes it look

like
> > this
> > 1555002,00 0 04 27,TELN NOT BILL
> > 3555007,00 0 06 00,CUSTOMER HAS > 1
> > 5555410,00 0 12 10,CUSTOMER HAS > 1
> > 6755012,00 0 12 06,CUSTOMER HAS > 1
> >
> > This output needs to be written to a file.
> > I have no idea how to start, if I split on a space " " the it will spit

the
> > third an fourth column up. The fourth column can basically be left

alone.
> >
> > Thanks for the help.
> >
> >

>
> If the data always has multiple spaces (ASCII 32) between fields, I'd
> try stripping the leading spaces and then converting >1 consecutive
> spaces to commas:
>
> perl -e -p 's/^ +//; s/ +/,/g' oldfile > newfile
>
> But I expect Shawn's substr solution to be more robust. Using unpack may
> be another useful approach.


I like this but I get nothing back in the new file. And I have no tabs they
are all spaces.


 
Reply With Quote
 
Tore Aursand
Guest
Posts: n/a
 
      09-21-2004
On Tue, 21 Sep 2004 00:40:17 +0200, Gunnar Hjalmarsson wrote:
>> If we skip everything that has got to do with the file(s), here's a
>> suggestion (untested);
>>
>> while ( <DATA> ) {
>> chomp; # Get rid of line breaks
>> s,^\s+,,; # Remove leading spaces
>> my @cols = split( /\s+{2,}/, $_ ); # Split on two (or more) spaces

> -----------------------------^^^^^
>
> Maybe you should have tested it...


You are so right, Gunnar, and I'm terribly sorry. The correct split()
should - of course - look like this:

my @cols = split( /\s{2,}/, $_ );

Still untested, though.


--
Tore Aursand <(E-Mail Removed)>
"I know not with what weapons World War 3 will be fought, but World War
4 will be fought with sticks and stones." (Albert Einstein)
 
Reply With Quote
 
Tore Aursand
Guest
Posts: n/a
 
      09-21-2004
On Mon, 20 Sep 2004 22:57:40 -0500, LHradowy wrote:
> my @cols=split(/\s+{2,}/,$_); #Split on two (or more) spaces


My fault. Don't split on '\s+{2,}', but on '\s{2,}';

my @cols = split( /\s{2,}/, $_ );


--
Tore Aursand <(E-Mail Removed)>
"I know not with what weapons World War 3 will be fought, but World War
4 will be fought with sticks and stones." (Albert Einstein)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
replace multiple spaces between words with single space. Shashank Khanvilkar Perl Misc 4 03-15-2005 10:58 AM
Why Python style guide (PEP-8) says 4 space indents instead of 8 space??? 8 space indents ever ok?? Christian Seberino Python 21 10-27-2003 04:20 PM
Re: Why Python style guide (PEP-8) says 4 space indents instead of8 space??? 8 space indents ever ok?? Ian Bicking Python 2 10-24-2003 11:15 AM
Re: Why Python style guide (PEP-8) says 4 space indents instead of8 space??? 8 space indents ever ok?? Ian Bicking Python 2 10-23-2003 07:07 AM
Stack space, global space, heap space Shuo Xiang C Programming 10 07-11-2003 07:30 PM



Advertisments