Join lines

Join lines

am 21.08.2007 18:45:05 von nickli2000

Hi,

I have a file with a lot of lines with a singer number like the
following:

111
222
333
444
555
6666
77777
888
9999
..........

How could I join 3 lines at a time and with a "," in between and at
the beginning of the next line, as in the following:

111,222,333
,444,555,6666
,77777,888,9999
.........

Thanks in advance.

Nick Li

Re: Join lines

am 21.08.2007 18:57:02 von Paul Lalli

On Aug 21, 12:45 pm, nickli2...@gmail.com wrote:
> I have a file with a lot of lines with a singer number like the
> following:
>
> 111
> 222
> 333
> 444
> 555
> 6666
> 77777
> 888
> 9999
> ..........
>
> How could I join 3 lines at a time and with a "," in between and at
> the beginning of the next line, as in the following:
>
> 111,222,333
> ,444,555,6666
> ,77777,888,9999
> .........

$ cat clpm.pl
#!/opt2/perl/bin/perl
use strict;
use warnings;

while (my $line = ) {
chomp $line;
print $line;
print "\n" if $. % 3 == 0;
print ",";
}

print "\n";

__DATA__
111
222
333
444
555
6666
77777
888
9999

$ ./clpm.pl
111,222,333
,444,555,6666
,77777,888,9999
,

$

Paul Lalli

Re: Join lines

am 21.08.2007 19:16:15 von Mirco Wahab

nickli2000@gmail.com wrote:
> I have a file with a lot of lines with a singer number like the
> following:
> 111
> 222
> 333
> 444
> 555
> 6666
> 77777
> 888
> 9999
> ..........
>
> How could I join 3 lines at a time and with a "," in between and at
> the beginning of the next line, as in the following:
> 111,222,333
> ,444,555,6666
> ,77777,888,9999

Is this Unix/Linux? Then do a simple:

perl -0777 -pe 's/\n/(++$n%3)?",":"\n,"/eg' lines.txt > commas.txt


if your file is 'lines.txt'.

In Win32, you have to change the quotes.

Regards

M.

Re: Join lines

am 21.08.2007 22:18:55 von nickli2000

On Aug 21, 1:16 pm, Mirco Wahab wrote:
> nickli2...@gmail.com wrote:
> > I have a file with a lot of lines with a singer number like the
> > following:
> > 111
> > 222
> > 333
> > 444
> > 555
> > 6666
> > 77777
> > 888
> > 9999
> > ..........
>
> > How could I join 3 lines at a time and with a "," in between and at
> > the beginning of the next line, as in the following:
> > 111,222,333
> > ,444,555,6666
> > ,77777,888,9999
>
> Is this Unix/Linux? Then do a simple:
>
> perl -0777 -pe 's/\n/(++$n%3)?",":"\n,"/eg' lines.txt > commas.txt
>
> if your file is 'lines.txt'.
>
> In Win32, you have to change the quotes.
>
> Regards
>
> M.- Hide quoted text -
>
> - Show quoted text -

Thanks for all your help.

Nick

Re: Join lines

am 22.08.2007 01:03:21 von Dummy

nickli2000@gmail.com wrote:
>
> I have a file with a lot of lines with a singer number like the
> following:
>
> 111
> 222
> 333
> 444
> 555
> 6666
> 77777
> 888
> 9999
> ..........
>
> How could I join 3 lines at a time and with a "," in between and at
> the beginning of the next line, as in the following:
>
> 111,222,333
> ,444,555,6666
> ,77777,888,9999
> .........

$ echo "111
222
333
444
555
6666
77777
888
9999" | perl -lpe'$\ = eof() ? "\n" : $. % 3 ? "," : "\n,"'
111,222,333
,444,555,6666
,77777,888,9999



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall