# Change line end

**URL:** <https://community.unix.com/t/change-line-end/370763>\
**Category:** Shell Programming and Scripting\
**Created:** [February 5, 2018, 9:19am UTC](https://community.unix.com/t/change-line-end/370763 "2018-02-05T09:19:57Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![API](https://community.unix.com/letter_avatar/api/32/6_449c533f586939b6e5f246632a46421f.png) [@API](https://community.unix.com/u/API)\
**Post date:** [February 5, 2018, 9:19am UTC](https://community.unix.com/t/change-line-end/370763/1 "2018-02-05T09:19:57Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/6_449c533f586939b6e5f246632a46421f.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [February 5, 2018, 9:34am UTC](https://community.unix.com/t/change-line-end/370763/2 "2018-02-05T09:34:31Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![API](https://community.unix.com/letter_avatar/api/32/6_449c533f586939b6e5f246632a46421f.png) [@API](https://community.unix.com/u/API)\
**Post date:** [February 5, 2018, 9:46am UTC](https://community.unix.com/t/change-line-end/370763/3 "2018-02-05T09:46:19Z")

</div>

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:

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

```

or with line visible line Ends:

```nohighlight
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:

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

```

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/6_449c533f586939b6e5f246632a46421f.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [February 5, 2018, 9:57am UTC](https://community.unix.com/t/change-line-end/370763/4 "2018-02-05T09:57:40Z")

</div>

Try

```nohighlight
sed 's/\r//g' file

```

OR

```nohighlight
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).

---

<div class="post-metadata">

**Author:** ![API](https://community.unix.com/letter_avatar/api/32/6_449c533f586939b6e5f246632a46421f.png) [@API](https://community.unix.com/u/API)\
**Post date:** [February 5, 2018, 10:04am UTC](https://community.unix.com/t/change-line-end/370763/5 "2018-02-05T10:04:54Z")

</div>

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

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

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

```

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/6_449c533f586939b6e5f246632a46421f.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [February 5, 2018, 10:14am UTC](https://community.unix.com/t/change-line-end/370763/6 "2018-02-05T10:14:32Z")

</div>

How about

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

```

---

<div class="post-metadata">

**Author:** ![API](https://community.unix.com/letter_avatar/api/32/6_449c533f586939b6e5f246632a46421f.png) [@API](https://community.unix.com/u/API)\
**Post date:** [February 5, 2018, 10:36am UTC](https://community.unix.com/t/change-line-end/370763/7 "2018-02-05T10:36:21Z")

</div>

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

```nohighlight
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...

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/6_449c533f586939b6e5f246632a46421f.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [February 5, 2018, 10:48am UTC](https://community.unix.com/t/change-line-end/370763/8 "2018-02-05T10:48:02Z")

</div>

`man regex` :

> [@](#):
>
> '^' (matching the null string at the beginning of a line), '$' (matching the null string at the end of a line),

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.

---

<div class="post-metadata">

**Author:** ![API](https://community.unix.com/letter_avatar/api/32/6_449c533f586939b6e5f246632a46421f.png) [@API](https://community.unix.com/u/API)\
**Post date:** [February 5, 2018, 10:53am UTC](https://community.unix.com/t/change-line-end/370763/9 "2018-02-05T10:53:32Z")

</div>

Ah yes! Thanks for your Explanation...
