0

Hello I am trying to add data in a file at the end but It doesn´t work I have a final.txt with this content

cat,dog,pig

I use cat file1.txt >> final.txt

but I obtain this

cat,dog,pig,car,plane,boat

and I want

cat,dog,pig
car,plane,boat

is it possible to obtain this?

Chem-man17
  • 1,700
  • 1
  • 12
  • 27
htmlpower
  • 169
  • 1
  • 16

2 Answers2

0
echo >> final.txt && cat file1.txt >> final.txt
Alvin Thompson
  • 5,388
  • 3
  • 26
  • 39
  • **Flaggers / reviewers:** [For code-only answers such as this one, downvote, don't delete!](//meta.stackoverflow.com/a/260413/2747593) (But IMHO, this answer is simple enough that it doesn't need much/any explanation, so it doesn't warrant even a downvote.) – Scott Weldon Nov 11 '16 at 19:39
  • you really should explain what you're doing instead of dumping code like this. – Jean-François Fabre Nov 11 '16 at 21:02
  • @Jean-FrançoisFabre: Jamil's version of this answer is exactly why you **shouldn't** always blindly add a text explanation of the command when the command itself is self-explanatory. His description simply added ambiguity where a reasonable person could believe the command will add a newline to the end of the file (it won't). Additionally, if the OP follows the advice he gives (always ending in a newline) the given answer will no longer work properly. – Alvin Thompson Nov 15 '16 at 14:47
  • I added that comment because your post was flagged. Note that I did not downvote. – Jean-François Fabre Nov 15 '16 at 15:05
  • @Jean-FrançoisFabre: yep, I did not assume it was you that down voted. – Alvin Thompson Nov 15 '16 at 15:09
  • and that's because there's no downvote at all on your answer :) – Jean-François Fabre Nov 15 '16 at 15:28
  • @Jean-FrançoisFabre: Ah, yep! – Alvin Thompson Nov 15 '16 at 15:55
0

Let's first talk about why your original code is not working, because this is something that can cause problems for you in the future: your original final.txt file did not end with a newline (that is, there was no extra blank line at the end of the file). That is a POSIX standard (see: Why should text files end with a newline?), and many programs will not work properly when dealing with a text file that does not end with a newline.

Thus, from now on it would be advisable to always end your text files with an newline.

Now, to solve the case at hand, you could run this code:

echo >> final.txt && cat file1.txt >> final.txt

That will fix the lack of a newline on the last line of the original text and thus allow the echo command to work as expected (add the data in a new line).

Important note: the echo command, by default (in most Bash versions), will add a newline at the end of whatever it is inserting. Therefore, when you run the command "echo >> final.txt", it will add a newline at the end of the last line. This is a built-in feature of echo, and that is why running the code above "fixes" the lack of a newline on the original file.

Community
  • 1
  • 1
Jamil Said
  • 2,033
  • 3
  • 15
  • 18
  • This command does **not** add a newline to the end of the file. It adds a newline before adding the new text. When done, the file does not have a newline at the end. – Alvin Thompson Nov 14 '16 at 18:24
  • The command does not NEED to add a newline at the end of the file. Instead, the command FIXES the lack of a newline on the end of the original last line on the file; then, the echo command works as expected and adds the data in a new line which ends with a newline. It works, test and you will see it. I edited the end of my answer for clarity. – Jamil Said Nov 15 '16 at 03:29
  • My point is your description strongly implies that the command adds a newline to the file. You wrote, "Thus, from now on it would be advisable to always end your text files with an newline" and immediately thereafter gave a command which breaks your own advice. – Alvin Thompson Nov 15 '16 at 14:22
  • Perhaps this is one reason why one **shouldn't** necessarily always give text descriptions of commands when the command is simple enough to be understood by itself? English (like most languages) can be ambiguous in its meaning; what you think you're saying may not be interpreted that way. Code, by definition, is never ambiguous in its meaning. The OP demonstrated that he understood shell commands well enough without the additional explanation. All you did was add uncertainty where there was none before. – Alvin Thompson Nov 15 '16 at 14:36
  • The echo command, by default (in most Bash versions), will add a newline at the end of whatever it is inserting. Therefore, when you run the command "echo >> final.txt", it will add a newline at the end of the last line. Make this test: If you run the command "echo >> empty.txt" 10 times in a completely blank file, when you open the file in a text editor and check the number of lines, you will see that echo added a newline for each time you run it. Therefore, I do stand correct on what I asserted, although that is not very clear because that is built in on echo and not everyone realizes that. – Jamil Said Nov 15 '16 at 23:28
  • How is your last statement relevant to anything? Hint: it's not. – Alvin Thompson Nov 17 '16 at 15:50