Why does insert (Unix) automatically add a new line?

I have two files (I renamed file1 / 2.csv to file1 / 2.txt):

file1     file2
-----     ------
a         1
b         2
...

When i use paste -d "," file1.txt file2.txt > file3.txt. The contents of file3.txt:

a
,1
b
,2

I wonder why it does not generate content, as shown below:

a,1
b,2
+4
source share
1 answer

The reason file1has \r\nline endings, as you can see in the dump xxd:

312030203020302030203020302030200d0a312031203120312030203020...
                                ^^^^
                                here

You know that the line ends because \r == 13 == 0x0dand \n == 10 == 0x0a.

So, the command pastedoes not know about \rbefore the end of the line, so it allows the end of each line from file1, but when you look at the contents file3you use a program that interprets \ras the end of the line.

, \n :

cat file1 | tr -d '\r' > file1-output

, dos2unix:

dos2unix file1
+2

Source: https://habr.com/ru/post/1692560/


All Articles