Author |
Message |
Tim Ellaw #1 / 12
|
 Format a one line file to get two columns
Please could someone tell me how to format a file containing one continuous line so that there's two columns? E.g. from this: -0.529766666666667 -8.89410873529075 -0.522816666666667 -8.628319980362 0 0 4.9781502 12.02 5.0119999 12.05375 to this: -0.529766666666667 -8.89410873529075 -0.522816666666667 -8.628319980362 0 0 4.9781502 12.02 5.0119999 12.05375
|
Mon, 09 May 2005 13:30:43 GMT |
|
 |
Koos Po #2 / 12
|
 Format a one line file to get two columns
Tim Ellaway wrote (Thursday 21 November 2002 13:30): Quote: > Please could someone tell me how to format a file containing one > continuous line so that there's two columns? > E.g. from this: > -0.529766666666667 -8.89410873529075 -0.522816666666667 > -8.628319980362 0 0 4.9781502 12.02 5.0119999 12.05375 > to this: > -0.529766666666667 -8.89410873529075 > -0.522816666666667 -8.628319980362 > 0 0 > 4.9781502 12.02 > 5.0119999 12.05375
If a space is unambiguously the seperator, lookup the documentation for "split". -- KP
|
Mon, 09 May 2005 14:30:40 GMT |
|
 |
Tad McClell #3 / 12
|
 Format a one line file to get two columns
Quote:
> Please could someone tell me how to format a file containing one > continuous line so that there's two columns?
But the data you showed was NOT on one continuous line. This will work whether the "parts" are on a single line or not. ---------------------------- #!/usr/bin/perl use warnings; use strict; { local $/; # enable "slurp mode" $_ = <DATA>; Quote: }
print "$1 $2\n" while /(\S+)\s+(\S+)/g; __DATA__ -0.529766666666667 -8.89410873529075 -0.522816666666667 -8.628319980362 0 0 4.9781502 12.02 5.0119999 12.05375 ---------------------------- -- Tad McClellan SGML consulting
Fort Worth, Texas
|
Mon, 09 May 2005 14:43:28 GMT |
|
 |
ebcha #4 / 12
|
 Format a one line file to get two columns
Quote: > Please could someone tell me how to format a file containing one > continuous line so that there's two columns? > E.g. from this: > -0.529766666666667 -8.89410873529075 -0.522816666666667 > -8.628319980362 0 0 4.9781502 12.02 5.0119999 12.05375 > to this: > -0.529766666666667 -8.89410873529075 > -0.522816666666667 -8.628319980362 > 0 0 > 4.9781502 12.02 > 5.0119999 12.05375
One way, not necessarily the most elegant. #!perl use warnings; use strict; my $line = <DATA>;
my $i = 0;
__DATA__ -0.529766666666667 -8.89410873529075 -0.522816666666667 -8.628319980362 0 0 4.9781502 12.02 5.0119999 12.05375 -- EBC
|
Mon, 09 May 2005 16:50:17 GMT |
|
 |
Scott Blankensh #5 / 12
|
 Format a one line file to get two columns
Quote:
> Please could someone tell me how to format a file containing one > continuous line so that there's two columns? > E.g. from this: > -0.529766666666667 -8.89410873529075 -0.522816666666667 > -8.628319980362 0 0 4.9781502 12.02 5.0119999 12.05375 > to this: > -0.529766666666667 -8.89410873529075 > -0.522816666666667 -8.628319980362 > 0 0 > 4.9781502 12.02 > 5.0119999 12.05375
#!/usr/local/bin/perl -w use strict; local $/; $_ = <DATA>; s/(\S+)\s+(\S+)\s+/$1 $2\n/g; print; __DATA__ -0.529766666666667 -8.89410873529075 -0.522816666666667 -8.628319980362 0 0 4.9781502 12.02 5.0119999 12.05375
|
Mon, 09 May 2005 22:03:35 GMT |
|
 |
David K. Wal #6 / 12
|
 Format a one line file to get two columns
Quote:
>> Please could someone tell me how to format a file containing one >> continuous line so that there's two columns? > But the data you showed was NOT on one continuous line. > This will work whether the "parts" are on a single line or not. > ---------------------------- > #!/usr/bin/perl > use warnings; > use strict; > { local $/; # enable "slurp mode" > $_ = <DATA>; > } > print "$1 $2\n" while /(\S+)\s+(\S+)/g; > __DATA__ > -0.529766666666667 -8.89410873529075 -0.522816666666667 > -8.628319980362 0 0 4.9781502 12.02 5.0119999 12.05375
Here's a longer way that that doesn't slurp the whole file. Yeah, the OP said it was all on one line, but I was pleased with this idea. I'm sure it's much slower than your way (all that splitting, pushing, and shifting), but for a large file with lots of relatively short lines it uses less memory, right? ---------------------------- use strict; use warnings;
while ( <DATA> ) {
Quote: }
# print any leftovers (in case the total number of parts is odd)
__DATA__ -0.529766666666667 -8.89410873529075 -0.522816666666667 -8.628319980362 0 0 4.9781502 12.02 5.0119999 12.05375 2.71828 ---------------------------- I put that 2.71828 in there make the number of parts odd. (e is a bit "odd", isn't it? :-) --
"Oook."
|
Mon, 09 May 2005 22:43:15 GMT |
|
 |
John W. Krah #7 / 12
|
 Format a one line file to get two columns
Quote:
> Please could someone tell me how to format a file containing one > continuous line so that there's two columns? > E.g. from this: > -0.529766666666667 -8.89410873529075 -0.522816666666667 > -8.628319980362 0 0 4.9781502 12.02 5.0119999 12.05375 > to this: > -0.529766666666667 -8.89410873529075 > -0.522816666666667 -8.628319980362 > 0 0 > 4.9781502 12.02 > 5.0119999 12.05375
perl -i~ -0777pe's/(\S+)\s+(\S+)\s+/$1 $2\n/g' yourfile John -- use Perl; program fulfillment
|
Mon, 09 May 2005 23:13:38 GMT |
|
 |
John W. Krah #8 / 12
|
 Format a one line file to get two columns
Quote:
> Here's a longer way that that doesn't slurp the whole file. > Yeah, the OP said it was all on one line, but I was pleased with this idea. > I'm sure it's much slower than your way (all that splitting, pushing, and > shifting), but for a large file with lots of relatively short lines it uses > less memory, right?
Ah, but if it _is_ all on one line then undefining $/ is redundant and your method would read the whole file as well. Quote: > ---------------------------- > use strict; > use warnings;
> while ( <DATA> ) {
> } > # print any leftovers (in case the total number of parts is odd)
> __DATA__ > -0.529766666666667 -8.89410873529075 -0.522816666666667 > -8.628319980362 0 0 4.9781502 12.02 5.0119999 12.05375 > 2.71828 > ---------------------------- > I put that 2.71828 in there make the number of parts odd. (e is a bit > "odd", isn't it? :-)
This will only read blocks of 1K #!/usr/bin/perl use warnings; use strict; $/ = \1024; while ( <> ) { s/\s+/--$| ? ' ' : "\n"/eg; s/\D*\z/\n/ if eof ARGV; print; } __END__ John -- use Perl; program fulfillment
|
Tue, 10 May 2005 03:06:25 GMT |
|
 |
ebcha #9 / 12
|
 Format a one line file to get two columns
Quote: >I put that 2.71828 in there make the number of parts odd. (e is a bit >"odd", isn't it? :-)
I'd say e transcends such notions as even or odd, though it's a natural mistake to think they seem integral to all numbers. Or is that not rational in the real world? These complex notions are beyond me. -- EBC
|
Tue, 10 May 2005 16:39:26 GMT |
|
 |
David K. Wal #10 / 12
|
 Format a one line file to get two columns
Quote:
>> Here's a longer way that that doesn't slurp the whole file. >> Yeah, the OP said it was all on one line, but I was pleased with >> this idea. I'm sure it's much slower than your way (all that >> splitting, pushing, and shifting), but for a large file with lots >> of relatively short lines it uses less memory, right? > Ah, but if it _is_ all on one line then undefining $/ is redundant > and your method would read the whole file as well.
Oh, granted -- I was just pleased to find One More Way To Do It. :-) Quote: > This will only read blocks of 1K > #!/usr/bin/perl > use warnings; > use strict; > $/ = \1024;
That's a feature of $/ I hadn't noticed before. I'll have to remember it. What's the advantage over sysread()? Quote: > while ( <> ) { > s/\s+/--$| ? ' ' : "\n"/eg; > s/\D*\z/\n/ if eof ARGV; > print; > } > __END__
Very clever. I like that much better than my little kluge or even the slurping way. I just had to play with it a little and get $| out of it. Switching the flushing on and off like that bothers me somehow. my $flipflop = 0; while ( <> ) { s/\s+/($flipflop = ++$flipflop % 2) ? ' ' : "\n"/eg; s/\S*\z/\n/ if eof ARGV; print; Quote: }
--
"Oook."
|
Tue, 10 May 2005 17:12:53 GMT |
|
 |
John W. Krah #11 / 12
|
 Format a one line file to get two columns
Quote:
> > This will only read blocks of 1K > > #!/usr/bin/perl > > use warnings; > > use strict; > > $/ = \1024; > That's a feature of $/ I hadn't noticed before. I'll have to > remember it. What's the advantage over sysread()?
It's shorter? :-) Quote: > > while ( <> ) { > > s/\s+/--$| ? ' ' : "\n"/eg; > > s/\D*\z/\n/ if eof ARGV; > > print; > > } > > __END__ > Very clever. I like that much better than my little kluge or even > the slurping way. > I just had to play with it a little and get $| out of it. Switching > the flushing on and off like that bothers me somehow. > my $flipflop = 0; > while ( <> ) { > s/\s+/($flipflop = ++$flipflop % 2) ? ' ' : "\n"/eg;
s/\s+/($flipflop %= 2)++ ? "\n" : ' '/eg; Quote: > s/\S*\z/\n/ if eof ARGV; > print; > }
John -- use Perl; program fulfillment
|
Tue, 10 May 2005 23:43:49 GMT |
|
 |
David K. Wal #12 / 12
|
 Format a one line file to get two columns
Quote: >> s/\s+/($flipflop = ++$flipflop % 2) ? ' ' : "\n"/eg; > s/\s+/($flipflop %= 2)++ ? "\n" : ' '/eg;
Smartaleck. :-) --
"Oook."
|
Wed, 11 May 2005 01:33:57 GMT |
|
|