Sed: Combining Multiple Lines into one


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sed: Combining Multiple Lines into one
# 1  
Old 09-10-2008
Sed: Combining Multiple Lines into one

Before I ask my actual question, is it going to be a problem that I want to run this process on a 15 Gig file that is ~140 million rows?

What I'm trying to do:
I have a file that looks like
Code:
Color,Type,Count,Day
Yellow,Full
5
Tuesday
Green,Half
6
Wednesday
Purple,Half
8
Tuesday
...

and my desired output is
Code:
Color,Type,Count,Day
Yellow,Full,5,Tuesday
Green,Half,6,Wednesday
Purple,Half,8,Tuesday
...

Leaving the title row alone, lines 2,3, and 4 should be combined into 1 line seperated by commas, same with lines 5,6, and 7, all the way to the last sets of lines in the file 139,999,997, 139,999,998 and 139,999,999.

Nothing I've tried with sed has allowed me to combine multiple lines into one line, since sed reads everything line by line. Some things I've read make this sound possible, but I have no idea how.

Thanks,
GoldFish
# 2  
Old 09-10-2008
sed reads a line at a time, so the actual number of lines is not a problem as such. If you do complex combining of lines, you might use up a lot of memory, but if your lines aren't particularly long and you don't need to process over long ranges, you should be fine even with a large input file.

You basically want to merge lines with commas until there are three commas, correct?

Code:
sed -e :0 -e '/,.*,.*,/b' -e N -e 's/\n/,/' -e b0 file

There are dialect differences between different sed implementations, but this works for me on Ubuntu Linux. In particular, the \n might be something you need to fiddle to get right.
# 3  
Old 09-10-2008
Code:
 sed '1n;N;N;s/\n/,/g' file
Color,Type,Count,Day
Yellow,Full,5,Tuesday
Green,Half,6,Wednesday
Purple,Half,8,Tuesday


Edit: Decoded a bit
1n; (skip first line)
N;N; (Read line (implied), Read another line, Read another line)
s/\n/,/g (substitute \n with , through to the end of the file)
# 4  
Old 09-10-2008
Thanks both for your solutions. Era, your solution takes a little long though... for my data it'd take a little less than 11 years based on a sample run I did (In my actual data set it has 22 commas in the first line of each set, so had to adjust yours according).

Thanks for the breaking it down too, arvonius!

I've learned a lot from both of these, thanks.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Combining multiple block of lines in one comma separated line

Hi Everyone, On my Linux box I have a text file having block of few lines and this block lines separated by one blank line. I would like to format and print these lines in such a way that this entire block of lines will come as single comma separated line & again next block of lines in next... (7 Replies)
Discussion started by: gr8_usk
7 Replies

2. UNIX for Dummies Questions & Answers

Need help combining txt files w/ multiple lines into csv single cell - also need data merge

:confused:Hello -- i just joined the forums. I am a complete noob -- only about 1 week into learning how to program anything... and starting with linux. I am working in Linux terminal. I have a folder with a bunch of txt files. Each file has several lines of html code. I want to combine... (2 Replies)
Discussion started by: jetsetter
2 Replies

3. Shell Programming and Scripting

combining multiple sed statements

I need to run a cronjob that will monitor a directory for files with a certain extension, when one appears I then need to run the below scripts How do I go about combining the following sed statements into one script? and also retain the original filename.? sed 's/71502FSC1206/\n&/g' # add a... (2 Replies)
Discussion started by: firefox2k2
2 Replies

4. Shell Programming and Scripting

Replace multiple lines through sed

Hi All, I have a input file as sample below <this is not starting of file> record line1 line2 line3 end line4 line5 record line6 line7 line8 my requirement is this, i want to select a pattern between first record and end, whatever is written between first record and end. and... (0 Replies)
Discussion started by: adgangwar
0 Replies

5. Programming

PERL:Combining multiple lines to single line

Hi All I need a small help for the below format in making a small script in Perl or Shell. I have a file in which a single line entries are broken into three line entries. Eg: I have a pen and notebook. All i want is to capture in a single line in a separate file. eg: I have a pen and... (4 Replies)
Discussion started by: Kalaiela
4 Replies

6. Shell Programming and Scripting

Combining multiple rows in single row based on certain condition using awk or sed

Hi, I'm using AIX(ksh shell). > cat temp.txt "a","b",0 "c",bc",0 "a1","b1",0 "cc","cb",1 "cc","b2",1 "bb","bc",2 I want the output as: "a","b","c","bc","a1","b1" "cc","cb","cc","b2" "bb","bc" I want to combine multiple lines into single line where third column is same. Is... (1 Reply)
Discussion started by: samuelray
1 Replies

7. Shell Programming and Scripting

searching thru or combining multiple lines in a unix file

This is the problem actually: This regex: egrep "low debug.*\".*\"" $dbDir/alarmNotification.log is looking for data between the two quotation marks: ".*\" When I hate data like this: low debug 2009/3/9 8:30:20.47 ICSNotificationAlarm Prodics01ics0003 IC... (0 Replies)
Discussion started by: ndedhia1
0 Replies

8. Shell Programming and Scripting

sed: working with multiple lines

Got another sed question :) My text block is I need to do the following: If (and only if) the line starting with 10002,11 is followed by a line starting with 10004,9 , insert the line 10003,9 between the 2 Thus, my output should be I tried but this gives me (the order... (3 Replies)
Discussion started by: orno
3 Replies

9. Shell Programming and Scripting

Use sed to merge multiple lines

Hi all: I have a file in which the contents are as following: ... This is a test ONE TWO Hello, world! XXX YYY CCC test again three, four five six seven world AAA BBB QQQ test eight, nine world (3 Replies)
Discussion started by: xb88
3 Replies

10. Shell Programming and Scripting

Combining multiple lines

I am fairly new to scripting. But I have been able to extract and format all of my information required into one file. My issue is that one character is on a separate line. I need to be able to add the character to the previous line. ex. abcdefghi 1 bcdefghij 3 cdefghijk 4 need to... (4 Replies)
Discussion started by: DUST
4 Replies
Login or Register to Ask a Question