Join lines

Discussion in 'Perl Misc' started by nickli2000@gmail.com, Aug 21, 2007.

  1. Guest

    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
     
    , Aug 21, 2007
    #1
    1. Advertising

  2. Paul Lalli Guest

    On Aug 21, 12:45 pm, 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
     
    Paul Lalli, Aug 21, 2007
    #2
    1. Advertising

  3. Mirco Wahab Guest

    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.
     
    Mirco Wahab, Aug 21, 2007
    #3
  4. Guest

    On Aug 21, 1:16 pm, Mirco Wahab <-halle.de> wrote:
    > 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
     
    , Aug 21, 2007
    #4
  5. 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
     
    John W. Krahn, Aug 22, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alan Silver
    Replies:
    0
    Views:
    912
    Alan Silver
    Jun 5, 2006
  2. googleboy
    Replies:
    1
    Views:
    952
    Benji York
    Oct 1, 2005
  3. nickli

    Using Perl to join lines in a file

    nickli, Nov 7, 2006, in forum: Perl Misc
    Replies:
    3
    Views:
    95
    John Bokma
    Nov 8, 2006
  4. Replies:
    4
    Views:
    155
    Clenna Lumina
    Jul 12, 2007
  5. julia

    join lines - perl command

    julia, Sep 7, 2009, in forum: Perl Misc
    Replies:
    2
    Views:
    113
Loading...

Share This Page