Format a one line file to get two columns 
Author Message
 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  
 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  
 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  
 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  
 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  
 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  
 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  
 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  
 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  
 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  
 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  
 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  
 
 [ 12 post ] 

 Relevant Pages 

1. Combining two files into a two-column file

2. Pattern matching spanning more than one line and substitution between two files -

3. Cannot make two changes on one line!

4. How to print file in two columns ?

5. perl script to swap the first two columns of the file /etc/hosts

6. perl script to swap the first two columns of the file /etc/hosts

7. line by line comparison of two files

8. search for one line and print that line and the one after it

9. Web Techniques Perl columns now available on-line (and more Unix Review columns)

10. **FORMAT Formatting columns/pages

11. Getting a script output one line at a time before the script ends executing

12. Web Techniques Perl columns now available on-line (and more Unix Review columns)

 

 
Powered by phpBB® Forum Software