-   Programming (
-   -   Merge lines in a file using sed (

arobic 06-08-2004 11:01 AM

Merge lines in a file using sed

I am just new to sed and I am scratching my head to write a script that would take the multiple lines of a file and would merge them together in a single line. I thought sed would be good to do that! For example, here are the few first line of my file:


and I want to get the following output:
98e_30788 98e_30802 98e_30825 ...

I found a script on the web that was merging two specific lines of a file but I just can't make it through the different commands of the script. I have searched for the threads in this forum about sed, but none of them were useful for my problem. If someone has any clue, please help me!


jim mcnamara 06-08-2004 11:27 AM

Try tr

cat myfile | tr -d '\n' > newfile

Hko 06-08-2004 11:32 AM

Jim's solution is much better, but if you want to do it the sed way:

Only if the file is not too big, sed can do it. The reason is that sed normally works in a line-by-line way. There is a way around this: sed's N command reads the next line and appends it to its buffer in memory. Then you can have sed delete the newline ('\n') between it by replacing it with a space. Then append the next line, and so on, until the entire file is in sed memory buffer with newlines replaced by spaces. If there's nothing left to read, sed prints the entire buffer to stdout:

sed -e :a -e N -e 's/\n/ /' -e ta yourfile.txt >newfile.txt
Here's the script line-by-line with comments:

:a        # label 'a' to jump back to later on
N        # Append next line to sed's buffer
s/\n/ /        # replace exactly one newline (\n) with a space
ta        # if the last replace command was succesfull, jump to label 'a'.
        # (if this does not happen, the entire file was read: end

arobic 06-08-2004 11:41 AM

Thanks a million guys!

I didn't even know about tr and now I do, with great pleasure :-)

Thanks for the explanation on the sed call! Sed is probably one of the most poorly documented program in the world! So it's always good to not only get the script but also know what each command does.

Hko 06-09-2004 07:00 AM


Sed is probably one of the most poorly documented program in the world!
While doing some thing with sed can be tricky, or difficlut to find out, I would not agree it's poorly documented: name a few.

arobic 06-09-2004 07:33 AM


I would not agree it's poorly documented
Thanks for the links, I will surely look at them!

I was only saying this because my search for documentation was mostly not succesful and the few sites I found were also saying that they had a hard time finding a good manual. That's all! :cool:

Hko 06-09-2004 08:05 AM

OK, no problem

lourencojunior 05-06-2008 03:07 PM

I have the same problem today and I solve it with:

Translating for your case, we have:
paste -s -d " " input.txt > output.txt

However, I do not know whether it is the best way to do it when input.txt is a very large file.


davidbalt 01-20-2012 02:11 PM

Used this today; thanks!
I used this example today to combine together the output of a Grep and funnel that output to another program:


cat LOGFILE | grep -E ".* MUST HAVE THIS TEXT .*" | perl -lpe '($_) = /(Part I'm Interested In)/' | sed -e :a -e N -e 's/\n/ /' -e ta | xargs MY PROGRAM
Just an amazingly useful tool.

All times are GMT -5. The time now is 12:51 PM.