For example, a normal ended file like this:
[root@localhost ~]$ cat -n temp.txt
1 My name is Ed Chen, you can call me Eddie. Here is my profile at : Google+. I live in Taipei, Taiwan and work as a Technical Supervisor at MDS System, Taiwan.
[root@localhost ~]$
There's only one line in this file. Please notice the position of the second Linux prompt. It's normal as we expect.
If I remove all the new line characters "n" (CRLF and CR only), you will see the difference.
[root@localhost ~]$ cat temp.txt | tr -d "n" > temp1.txt
[root@localhost ~]$ cat -n temp1.txt
1 My name is Ed Chen, you can call me Eddie. Here is my profile at : Google+. I live in Taipei, Taiwan and work as a Technical Supervisor at MDS System, Taiwan.[root@localhost ~]$
You can see the position of second Linux prompt stick with the end of the file. Which means the CR at the end of the file is removed also.
Mostly, it's a no harm no foul file and won't be a problem. But if you plans to append other files under files like this, it won't start a new line. That's a problem.
[root@localhost ~]$ cat temp.txt >> temp1.txt
[root@localhost ~]$ cat -n temp1.txt
1 My name is Ed Chen, you can call me Eddie. Here is my profile at : Google+. I live in Taipei, Taiwan and work as a Technical Supervisor at MDS System, Taiwan.My name is Ed Chen, you can call me Eddie. Here is my profile at : Google+. I live in Taipei, Taiwan and work as a Technical Supervisor at MDS System, Taiwan.
[root@localhost ~]$
As you can see, I appended other file to this one, it's supposed to be two lines in the file, but it turned out only one line there. They stick together because of no CR at the end of the target file.
To solve this, we can add a CR to the target file.
[root@localhost ~]$ echo $'r' >> temp1.txt
Actually, we added an empty line and a CR, not only CR to the file, you may refer to this post for more details:
How to Distinguish Carriage Returns CR, Line Feeds LF, and New Lines CRLF on Linux
Let's finish our job to append files.
[root@localhost ~]$ cat temp.txt | tr -d "n" > temp1.txt
[root@localhost ~]$ echo $'r' >> temp1.txt
[root@localhost ~]$ cat -n temp1.txt
1 My name is Ed Chen, you can call me Eddie. Here is my profile at : Google+. I live in Taipei, Taiwan and work as a Technical Supervisor at MDS System, Taiwan.
[root@localhost ~]$ cat temp.txt >> temp1.txt
[root@localhost ~]$ cat -n temp1.txt
1 My name is Ed Chen, you can call me Eddie. Here is my profile at : Google+. I live in Taipei, Taiwan and work as a Technical Supervisor at MDS System, Taiwan.
2 My name is Ed Chen, you can call me Eddie. Here is my profile at : Google+. I live in Taipei, Taiwan and work as a Technical Supervisor at MDS System, Taiwan.
[root@localhost ~]$
It becomes normal.
In this tutorial, we have learned the following things:
- How to remove a new line "n" (CRLF and CR) by tr.
- How to know a CR is missing at the end of a file.
- How to add a CR "r" to a file in order to append other files.