Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Join lines

Reply
Thread Tools

Join lines

 
 
nickli2000@gmail.com
Guest
Posts: n/a
 
      08-21-2007
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

 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      08-21-2007
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 = <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

 
Reply With Quote
 
 
 
 
Mirco Wahab
Guest
Posts: n/a
 
      08-21-2007
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.


 
Reply With Quote
 
nickli2000@gmail.com
Guest
Posts: n/a
 
      08-21-2007
On Aug 21, 1:16 pm, Mirco Wahab <wa...@chemie.uni-halle.de> 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

 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      08-21-2007
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
join lines - perl command julia Perl Misc 2 09-07-2009 07:42 PM
Using xargs perl to join lines charissaf@gmail.com Perl Misc 4 07-12-2007 04:35 PM
Using Perl to join lines in a file nickli Perl Misc 3 11-08-2006 10:07 PM
You must join New York Paid To Read !!!!!!! NewYork PTR is online!!! JOIN NOW!!!!!! Alan Silver ASP .Net 0 06-05-2006 03:27 PM
list.join()... re.join()...? Do they exist? (newbie questions...) googleboy Python 1 10-01-2005 12:56 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57