LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-08-2004, 11:01 AM   #1
arobic
Member
 
Registered: Jul 2003
Location: Geneva, Switzerland
Distribution: Debian 3.1, SLC3 (based on RHEL)
Posts: 84

Rep: Reputation: 15
Merge lines in a file using sed


Hi!

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:

98e_30788
98e_30802
98e_30825
98e_30849
98e_30888
98e_30903
98e_30923
98e_30943
98e_30956

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!



 
Old 06-08-2004, 11:27 AM   #2
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
Try tr
Code:
cat myfile | tr -d '\n' > newfile
 
1 members found this post helpful.
Old 06-08-2004, 11:32 AM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
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:
Code:
sed -e :a -e N -e 's/\n/ /' -e ta yourfile.txt >newfile.txt
Here's the script line-by-line with comments:
Code:
: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

Last edited by Hko; 06-08-2004 at 11:34 AM.
 
Old 06-08-2004, 11:41 AM   #4
arobic
Member
 
Registered: Jul 2003
Location: Geneva, Switzerland
Distribution: Debian 3.1, SLC3 (based on RHEL)
Posts: 84

Original Poster
Rep: Reputation: 15
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.
 
Old 06-09-2004, 07:00 AM   #5
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
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:

http://www.gnu.org/software/sed/manu...e/sed_toc.html
http://sed.sourceforge.net/grabbag/tutorials/
http://www-h.eng.cam.ac.uk/help/tpl/unix/sed.html

...to name a few.
 
Old 06-09-2004, 07:33 AM   #6
arobic
Member
 
Registered: Jul 2003
Location: Geneva, Switzerland
Distribution: Debian 3.1, SLC3 (based on RHEL)
Posts: 84

Original Poster
Rep: Reputation: 15
Quote:
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!
 
Old 06-09-2004, 08:05 AM   #7
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
OK, no problem
 
Old 05-06-2008, 03:07 PM   #8
lourencojunior
LQ Newbie
 
Registered: May 2008
Posts: 8

Rep: Reputation: 0
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.

Kindly,
LJr.
 
Old 01-20-2012, 02:11 PM   #9
davidbalt
Member
 
Registered: Feb 2004
Distribution: Gentoo -- always from stage1
Posts: 85

Rep: Reputation: 15
Used this today; thanks!

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

Code:
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Join all lines using sed chipix Programming 3 04-03-2007 09:55 AM
Is there a command to merge two files as two columns of one file? davee Linux - General 2 07-19-2005 10:52 AM
Removing duplicate lines with sed tireseas Programming 10 01-12-2005 03:27 AM
replacement with sed: replace pattern with multiple lines Hcman Programming 5 11-18-2004 07:40 AM
merge multiple lines of a single file into one line groverrajiv Linux - Newbie 4 05-26-2004 02:38 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:59 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration