Results 1 to 2 of 2
I have a giant text file with thousands of line.
I want to merge selected multiple lines into a single line.
Lines that begin with a coma will be merged ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 09-07-2006 #1Linux Newbie
- Join Date
- Oct 2004
- Posts
- 114
Merging Selected Lines in a Text File
I have a giant text file with thousands of line.
I want to merge selected multiple lines into a single line.
Lines that begin with a coma will be merged with previous line.
Lines that begin with other characters will not be merged with previous line.
Example: from:
aaaaa
, bbbb
, ccccc
dddd
, eeee
To:
aaaa, bbbb, cccc
dddd, eeee
How to do this operation? Can I use sed command?
Thank you.
- 09-07-2006 #2Linux Engineer
- Join Date
- Apr 2006
- Location
- Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
- Posts
- 1,199
Hi, xxqq.
Brute force:
Results on your data file:Code:#!/usr/bin/perl # @(#) p1 Demonstrate match including newlines. use warnings; use strict; # slurp in entire file. # Perl Best Practices, page 213. my $file = do { local $/; <> }; # replace "newline," with ",". # PBP, p 235 - 241 $file =~ s/\n,/,/xmsg; print $file;
Drawback: memory intensive ... cheers, drlCode:./p1 data1 aaaaa, bbbb, ccccc dddd, eeee
Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
We look forward to helping you with the challenge of the other 10%.
( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )


Reply With Quote
