[opensource] Command line question

Jim Dinan dinan at cse.ohio-state.edu
Sun Jun 4 11:35:59 EDT 2006


William R. Lorenz wrote:
> The following code will join together every 15 lines:
> 
>     #!/usr/bin/perl
> 
>     open(FILEI,"<file.i"); # in
>     open(FILEO,">file.o"); # out
> 
>     for ($i = 0; <FILEI>; $i++)
>     {
>             if ($i ne 15)
>             {
>                     print FILEO $_;
> 
>             } else
>             {
>                     $i = 0;
>             }
>     }
> 
>     close(FILEI);
>     close(FILEO);
> 
> If you want to specialize it for newlines, that should be easy enough.
> 
> Primitive, but it works. ;)  Hope it helps.

It looks like that script skips every 16th line rather than joining
every 15.  If you want to do the latter, you might need to do something
like:

---(snip)---

if ($i ne 15)
{
	chomp;
	print FILEO "$_ ";
} else
{
	print FILEO;
	$i = 0;
}

---(snip)---

... and just to be a pain in the ass, here is the compulsory one-liner:

perl -pe '($. % 16 != 0) and s/\n/ /'

:)

 - jim.

-- 
James Dinan <dinan at cse.ohio-state.edu>

Graduate RA - Computer Science and Engineering
              The Ohio State University


More information about the Opensource mailing list