# sed removing carriage return and newline

**URL:** <https://community.unix.com/t/sed-removing-carriage-return-and-newline/148856>\
**Category:** Shell Programming and Scripting\
**Created:** [May 5, 2004, 4:22am UTC](https://community.unix.com/t/sed-removing-carriage-return-and-newline/148856 "2004-05-05T04:22:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mored](https://community.unix.com/letter_avatar/mored/32/5_5575768a8748004e209b776fc1b2916d.png) [@mored](https://community.unix.com/u/mored)\
**Post date:** [May 5, 2004, 4:22am UTC](https://community.unix.com/t/sed-removing-carriage-return-and-newline/148856/1 "2004-05-05T04:22:26Z")

</div>

Hi,

I'm not very familiar with unix shell. I want to replace the combination of two carriage returns and one newline with one carriage return and one newline. I think the best way to do this is to use sed. I tried something like this:

sed -e "s#\[/r][/r][/n]#\[/r][/n]#g" file.txt

but it doesn't work.  
Thanx in advance.

Greetings

Gina

---

<div class="post-metadata">

**Author:** ![butterfm](https://community.unix.com/letter_avatar/butterfm/32/5_5575768a8748004e209b776fc1b2916d.png) [@butterfm](https://community.unix.com/u/butterfm)\
**Post date:** [May 6, 2004, 8:16am UTC](https://community.unix.com/t/sed-removing-carriage-return-and-newline/148856/2 "2004-05-06T08:16:52Z")

</div>

Hi Gina,

Try something along these lines

sed 's!\\r\\r\\n!\\r\\n!g' file.txt  
Hope this helps.

Matt.

---

<div class="post-metadata">

**Author:** ![Ygor](https://community.unix.com/user_avatar/community.unix.com/ygor/32/104_2.png) [@Ygor](https://community.unix.com/u/Ygor)\
**Post date:** [May 6, 2004, 12:28pm UTC](https://community.unix.com/t/sed-removing-carriage-return-and-newline/148856/3 "2004-05-06T12:28:27Z")

</div>

sed uses $ as the end of line marker (i.e newline), so use something like...

```nohighlight
$ CR=`printf "\r"`
$ printf "\r\r\n" | sed "s/$CR$CR$/$CR/g" | od -hc
0000000 0d0a
         \r \n
0000002

```
