Join lines

N

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
 
P

Paul Lalli

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 = <DATA>) {
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
 
M

Mirco Wahab

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.
 
N

nickli2000

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
 
J

John W. Krahn

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top