Using Perl to join lines in a file

N

nickli

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
 
J

John W. Krahn

nickli said:
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
 
N

nickli

Thanks a lot!

nickli said:
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
 

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,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top