Change line end

Hello all,

what I would like to have sounds quite simple...

I have a file where the records have a line ending like (CR, LF, CR/LF, LFLF, ...) - so I do not know.

In result I would like to have a file with UNIX line endings -> LF

Maybe I can do it with a sed - but I do not know how to tell the sed that he should take the complete line ending and change it to a LF?

---------- Post updated at 03:19 PM ---------- Previous update was at 03:18 PM ----------

The file could have different line-endings!

Sure a <CR> means a line end in your file? While <LF> (*nix) and <CR><LF> (DOS / Windows) usually do, a <CR> alone frequently means a "local line break" in a field. And <LF><LF> can stand for two lines, one with a text and one empty.

Some sample input data would halp as well.

Normally each record within a file have a line end like <LF>. But sometimes some lines will be changed at Windows platform. Therefor it could happen that you have all different possibilities - as described above.

To place it here in a code-Fragment is not possible, because you won't see the line ends, or?

But the a possible file is like this:

Discont;23.98;34
Discont;23.09;34
Discont;11.09;54
Discont;11.43;66

or with line visible line Ends:

Discont;23.98;34\n
 Discont;23.09;34\r\n
 Discont;11.09;54\n
 Discont;11.43;66\n\r\r

The result should be:

Discont;23.98;34\n
 Discont;23.09;34\n
 Discont;11.09;54\n
 Discont;11.43;66\n

Try

sed 's/\r//g' file

OR

sed  's/^M//g' file

The ^M represents the <CR> (single!) char and can be produced (at least in my shell) by pressing ^V (Ctrl V) followed by ^M (Ctrl M).

Yes, you are right. As I see my examples - it would work.

But, what if I have a line - like (I forgot this case):

Discont;23.09;34\n\r
 Discont;11.09;54\r
 Discont;11.43;66\n

How about

sed 's/\r\+/\n/g; s/^\n\|\n$//g' file

Thats very good. It seems to be working...
But I have a question to the second command:

s/^\n\|\n$//g' 

You are replacing all LFs to nothing? Then no LF would be there anymore? Ok, it works, but I do not understand it...

man regex :

Above means: a line feed at the begin-of-line or at end-of-line will be removed. You have to know that in sed , the line end is implicitly known, no <LF> in pattern space. Thus a really existing "extra" <LF> would mean an empty line and needs to be removed.

Ah yes! Thanks for your Explanation...