Combine the lines from separate text files

Hi All,

I have three separate text files which has only one line and i want to combine these lines in one text file which will have three lines.

 
cat file1.txt

abc

 
cat file2.txt

1265 6589 1367

cat file3.txt

0.98 0.36 0.5

So, I want to see these three lines in the output.txt look like

 
cat output.txt

abc
1265 6589 1367
0.98 0.36 0.5

i used

 
cat file1.txt file2.txt file3.txt > output.txt

which has only 1 line but i want 3 lines.

Thanks

cat fil*.txt >>output.txt

Too simple?

I think i should add a new line to the end of each file1.txt file2.txt and file3.txt. Then cat command can work? How can i add a new line to the end of the line of each files?

Thanks

Unless you know for a fact the files don't end in newlines, they almost certainly do.

Did you try it? Did it work or not?

Have you tried the given code?

Ok. I figured it out.

 
sed 's/.$/\n/g' file1.txt
sed 's/.$/\n/g' file2.txt
sed 's/.$/\n/g' file3.txt
cat file1.txt file2.txt file3.txt > output.txt

Thanks.

Those seds just print to the terminal because you don't save their output anywhere. They don't "fix" your files. Chop them out.

Since it works, your files obviously don't need fixing. The original suggestion works.

1 Like

Hi Corona688,

Yes, you are right i thought that i fixed it but in fact i couldn't.

 
sed 's/.$/\n/g' file1.txt > out_file1.txt

it replace the last character of the line by the new line. In fact, i want to paste a newline to the end of the line. How can i do that?

Thanks,

The 'cat' solutions proposed previously should have provided the desired results. Either your files are not what you think they are OR you're not executing what's been proposed.
Post the output of the following:

cat -vet file*

We're already on page 2 and we still don't know if you ever even tried the answer given in post 2. :wall: If it didn't work, could you please at least say so? We can't see your computer from here.

Did you edit these files in Notepad? You'll have filled them with garbage carriage returns.

tr -d '\r' < wingarbage.txt > fixed.txt