Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove \r character with sed

Tags:

regex

sed

Very simply I have a file that has \r\n at every line break.

aaaa\r\nbbbb\r\ncccc

I would like to remove the \r character while leaving the \n in place.

I could do this easily in python but it seems to be more elegant with a simple sed command. Is this possible? What expression would do the trick? I can't seem to find any such solution online.

Thank you

like image 371
Malonge Avatar asked Apr 24 '15 18:04

Malonge


People also ask

What does \r means in a shell script?

The \r issue suggests you have edited the script file on a Windows machine. Windows uses \r\n as the line terminator while Linux (and most other operating systems) use \n alone. When you edit a file in Windows, Windows will add \r to the end of the lines and that will break your scripts.

What \r does in Linux?

The character '\r' is carriage return. It returns the cursor to the start of the line.


2 Answers

You should be able to do it with sed like this:

sed 's/\r//g' orig.txt > modified.txt
like image 56
Wiktor Stribiżew Avatar answered Sep 22 '22 07:09

Wiktor Stribiżew


In case you are using the terminal on a mac, use this instead to be able to recognize \r

sed "s/$(printf '\r')\$//" orig.txt > modified.txt

like image 43
Carina Chen Avatar answered Sep 22 '22 07:09

Carina Chen