Take the 2-minute tour ×
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

8 Answers 8

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

GEDIT:

Search and replace \n with a space ' '.
You can get the replace window by going to 'Search'->'Replace'
or via the keybpard shortcut Ctrl+H

See screenshot below:

Your original text is on lines 1-14.
The result is on line 16.

enter image description here

share|improve this answer

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! –  user76204 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

Pure bash solution:

while read i; do printf '%s ' "$i"; done < file.txt > outfile.txt
share|improve this answer

If it were me I'd just open it in vim and press Shift+J a few times.

share|improve this answer

Try this

sed -e :a -e '/$/N; s/\n/\\n/; ta' [filename]

http://anandsekar.github.io/joining-all-lines-in-a-file-using-sed/

share|improve this answer
    
the true one is sed -e :a -e '/$/N; s/\n//; ta' [Filename] –  KasiyA Aug 20 at 11:23
2  
Could you give more details / explanation about all these switches ? It can make to understand better why it is working or not. –  Benoit Aug 20 at 11:28

Try with this one:

sed -e :a -e '/$/N; s/\n/ /; ta' test.txt

-e : It allows to write a sed program in several parts. For example, a sed program with two substitution rules could be written as sed -e 's/one/two/' -e 's/three/four' instead of sed 's/one/two/;s/three/four'. It makes it more readable.

In this one-liner the first -e creates a label called a. The : command followed by a name crates a named label.

The second -e uses a new command t. The t command branches to a named label if the last substitute command modified pattern space. This branching technique can be used to create loops in sed.

see this sample of sed:

sed 's/foo/bar/'

This is the simplest sed one-liner possible. It uses the substitute command and applies it once on each line. It substitutes string "foo" with "bar". that I used in (... ; s/\n/ / ; ...) at first line, it means replace \n with ' ' whitespace)

Ok

then see this sample of sed now:

sed '$!N;s/\n/ /'

It's similar to sed '/$/N;s/\n/ /' that I used in (/$/N; s/\n/ / ; ...) at first line, it means join every pair of lines with a whitespace.

This one-liner joins two consecutive lines with the N command. They get joined with a \n character between them. The substitute command replaces this newline with a space, thus joining every pair of lines with a whitespace.

Here is the output of running this command in my test.txt file:

User@test-pc:~$ sed '/$/N;s/\n/ /' test.txt

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"

Alright, final command become this sed -e :a -e '/$/N; s/\n/ /; ta' test.txt:

The first expression :a creates a named label a. The second expression looks to see if the current line ends with a \n. If it does, it joins it with the line following it using the N command. Then the \n and the newline between joined lines get erased with s/\n/ / command. If the substitution was successful we branch to the beginning of expression and do the same again, in hope that we might have another \n. If the substitution was not successful, the line did not end with a \n and we print it out.

Here is the the output or running this one-liner:

User@test-pc:~$ sed -e :a -e '/$/N; s/\n/ /; ta' test.txt

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"

Helping Article

share|improve this answer

Here it is. It's another solution and simple easy.

echo $(cat Input.txt) > Output.txt
share|improve this answer

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.