Tell me more ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I want to get all lines in a text into one line. I'm a beginner at coding trying to learn by doing. I've spent four hours trying to solve this problem. I know there's a simple solution to this problem. Here's what I've been trying.

sed -e 'N;s/\n//' myfile.txt #Does nothing

sed -e :a -e N -e 's/\n/ /' -e ta myfile.txt #output all messed up and I can't make head nor tail of the syntax

cat myfile.txt | tr -d '\n' > myfile.txt # Deletes all lines

Here's the text file:

500212
262578-4-4
23200
GRIFFITH LABORATORIES LTD
GRIFFITH LABORATORIES
SOUTH DUBLIN COUNTY COUNCIL
OFFICE
OFFICE (INDUSTRIAL)
List Rateable
2 Pineview Industrial Estate
Firhouse Road
Knocklyon
31 Dec 2007
01 Jan 2008"   

I can't figure out where I've gone wrong....

share|improve this question

2 Answers

tr as you used it should work and is the simplest -- you just need to output to another file. If you use the input file as output, the result is an empty file as you observed;

cat myfile.txt | tr -d '\n' > oneline.txt
share|improve this answer
Thanks for your help but it's still not working. I know it's saomething basic and simple. The command you gave deleted all lines. I'm going to post the file 500212 262578-4-4 23200 GRIFFITH LABORATORIES LTD GRIFFITH LABORATORIES SOUTH DUBLIN COUNTY COUNCIL OFFICE OFFICE (INDUSTRIAL) List Rateable 2 Pineview Industrial Estate Firhouse Road Knocklyon 31 Dec 2007 01 Jan 2008 – John Jul 16 '12 at 11:30
@JOhn: What is the result or error? – izx Jul 16 '12 at 11:31
I was going to post by editing my answer. After I used the command the file was blank. It looked like this in the terminal "cat reval_details.asp?Pno=500211.txt | tr -d '\n' > reval_details.asp?Pno=500211.txt". Thanks again. – John Jul 16 '12 at 11:49
@John: You cannot use the same output file name!! (see answer). Please try cat reval_details.asp?Pno=500211.txt | tr -d '\n' > new_reval_details.asp?Pno=500211.txt – izx Jul 16 '12 at 11:53
Sorry about that. The command cat reval_details.asp?Pno=500213.txt | tr -d '\n' > reval_details.asp?Pno=500213.txt deleted the file. I tried cat reval_details.asp?Pno=500213.txt | tr -d '\n' > newfile.txt. newfile.txt is the same as the old 500213 262578-5-8 7900 PATRICK SPILLANE SECTOR SECURITY SOUTH DUBLIN COUNTY COUNCIL OFFICE OFFICE (INDUSTRIAL) List Rateable 5 Pineview Industrial Estate Firhouse Road Knocklyon 31 Dec 2007 01 Jan 2008. Cheers – John Jul 16 '12 at 12:05
show 2 more comments

I think you were simply forgetting that you needed to tell sed to redirect the output of yourfile.txt to the desired result, newfile.txt. This appears to be the command you need, but only if the files you are trying to merge are not too big for sed's buffers: sed -e :a -e N -e 's/\n/ /' -e ta yourfile.txt >newfile.txt. Credit to another forum here, where they discuss sed's capabilities. I have tested the command and it worked for me.

share|improve this answer
Actually, sed with the -i switch will modify the original file inline, so there's no need to output to another file. Be careful though! – izx Jul 16 '12 at 8:17
I tried sed -e :a -e N -e 's/\n/ /' -e ta reval_details.asp?Pno=500214.txt 01 Jan 2008addustrial Estate sed -i :a -e N -e 's/\n/ /' -e ta reval_details.asp?Pno=500214.txt sed: can't find label for jump to a' sed -i :a -e N -i 's/\n/ /' -e ta reval_details.asp?Pno=500214.txt sed: can't find label for jump to a' sed -e :a -e N -i 's/\n/ /' -e ta reval_details.asp?Pno=500214.txt sed: can't read s/\n/ /: No such file or directory #Any ideas. – John Jul 16 '12 at 12:09
@Mik I've still had no luck but thanks for the reply. – John Jul 16 '12 at 12:13
I copy and pasted your text into a text editor and saved the file then used my sed command and it made it all appear on one line when opened in nano; however if you open out the file using cat in the terminal it will be wrapped and not appear to be just one line, so perhaps that's what's happening. Also, you don't need a' before sed and I think you've added some other unnecessary elements to the command above, so try sed -e :a -e N -e 's/\n/ /' -e ta yourfile.txt >newfile.txt The line is so long it will appear wrapped on screen, however, unless you have a 30+ inch monitor! – Mik Jul 16 '12 at 12:23
@Mik Cheers for the post. I typed my data into a text editor saved it and ran ' sed -e :a -e N -e 's/\n/ /' -e ta abc.txt >abc1.txt ' and it worked. perfectly. Hallelujah. I then typed ' sed -e :a -e N -e 's/\n/ /' -e ta reval_details.asp?Pno=500215.txt >Pno=500215.txt ' and every line apart from the first had a space added to the start of the line. I appreciate the effort you put in but I think I'll move on to octave and try and process the data there. – John Jul 16 '12 at 13:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.