Using Perl to join lines in a file

Discussion in 'Perl Misc' started by nickli, Nov 7, 2006.

  1. nickli

    nickli Guest

    Hi,

    I have a file with 100 lines each with one record on each line. The
    file is as follows:

    86274886
    81459795
    78113464
    87882848
    78286396
    77618550
    84180945
    74310227
    69806585
    74111006
    80947497
    80404769
    80377684
    80259589
    68195927
    67187136
    67345008
    78745575
    77970192
    77937896
    85872999
    85954776
    85930231
    85927774
    85887197
    85911310
    85577810
    85645926
    85645257
    85609385
    85428783
    85455622
    86143491
    86143435
    85501684
    86168358
    86143964
    86036286
    85830781
    86159082
    98624907
    98484232
    98506935
    21986088
    103299004
    101901712
    21246519
    69548444
    69534265
    98523400
    51579566
    63869631
    47500761
    63974605
    63727180
    48348160
    51465556
    51468035
    50245797
    49894334
    90940723
    16226272
    16222857
    16215465
    16199374
    16184704
    16355800
    16320328
    54274394
    54210417
    79781445
    79778999
    79772052
    79720217
    79719727
    80117560
    80102759
    80152647
    80116881
    79976145
    19592974
    19587585
    19585909
    19574981
    19574860
    19574560
    19659755
    19654534
    19636650
    19630077
    17823273
    17817739
    17764284
    17728035
    17694825
    17766363
    17730554
    17675729
    17803920
    17773924

    I would like to put 5 records on a line, separated by commas, with
    a total of 20 lines:

    ,86274886,81459795,78113464,87882848,78286396
    ,80947497,80404769,80377684,80259589,68195927
    ......................................
    ......................................

    I can do this with shell script using split and paste commands. I
    would like to ask for help as I am new to Perl.

    Thanks in advance.

    Nick Li
    nickli, Nov 7, 2006
    #1
    1. Advertising

  2. nickli wrote:
    >
    > I have a file with 100 lines each with one record on each line. The
    > file is as follows:
    >
    > 86274886
    > 81459795
    > 78113464


    [ snip ]

    > 17675729
    > 17803920
    > 17773924
    >
    > I would like to put 5 records on a line, separated by commas, with
    > a total of 20 lines:
    >
    > ,86274886,81459795,78113464,87882848,78286396
    > ,80947497,80404769,80377684,80259589,68195927
    > ......................................
    > ......................................


    perl -lpe'$\=$.%5?",":$/' yourfile



    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, Nov 7, 2006
    #2
    1. Advertising

  3. nickli

    nickli Guest

    Thanks a lot!

    On Nov 7, 10:10 am, "John W. Krahn" <> wrote:
    > nickli wrote:
    >
    > > I have a file with 100 lines each with one record on each line. The
    > > file is as follows:

    >
    > > 86274886
    > > 81459795
    > > 78113464[ snip ]

    >
    > > 17675729
    > > 17803920
    > > 17773924

    >
    > > I would like to put 5 records on a line, separated by commas, with
    > > a total of 20 lines:

    >
    > > ,86274886,81459795,78113464,87882848,78286396
    > > ,80947497,80404769,80377684,80259589,68195927
    > > ......................................
    > > ......................................perl -lpe'$\=$.%5?",":$/' yourfile

    >
    > 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
    nickli, Nov 8, 2006
    #3
  4. nickli

    John Bokma Guest

    John Bokma, Nov 8, 2006
    #4
    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:
    878
    Alan Silver
    Jun 5, 2006
  2. googleboy
    Replies:
    1
    Views:
    918
    Benji York
    Oct 1, 2005
  3. Replies:
    4
    Views:
    141
    Clenna Lumina
    Jul 12, 2007
  4. julia

    join lines - perl command

    julia, Sep 7, 2009, in forum: Perl Misc
    Replies:
    2
    Views:
    103
  5. Ninja Li

    Join every two lines of a file

    Ninja Li, Dec 3, 2009, in forum: Perl Misc
    Replies:
    4
    Views:
    130
    John W. Krahn
    Dec 3, 2009
Loading...

Share This Page