Malonge
Malonge

Reputation: 2040

How to remove \r character with 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

Upvotes: 12

Views: 26077

Answers (3)

nessa.gp
nessa.gp

Reputation: 1863

Adding to previous answers, if you want to modify the file instead of creating a new one:

sed -i 's/\r$//' file.txt

In case you want to store a backup file, let's say with an extension .bak:

sed -i.bak 's/\r$//' file.txt

This modifies file.txt and creates a backup file named file.txt.bak.

Upvotes: 4

Carina Chen
Carina Chen

Reputation: 43

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

Upvotes: 2

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626689

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

sed 's/\r//g' orig.txt > modified.txt

Upvotes: 21

Related Questions