What is the simplest way to remove all the carriage returns \r
from a file in Unix?
相关问题
- Why should we check WIFEXITED after wait in order
- UNIX Bash - Removing double quotes from specific s
- bash delete line condition
- Trying to make a permanent Alias - UNIX
- Generating signed XPI via jpm failed
See tr(1)
The simplest way on Linux is, in my humble opinion,
The strong quotes around the substitution operator
's/\r//'
are essential. Without them the shell will interpret\r
as an escape+r and reduce it to a plainr
, and remove all lower caser
. That's why the answer given above in 2009 by Rob doesn't work.And adding the
/g
modifier ensures that even multiple\r
will be removed, and not only the first one.sed -i s/\r// <filename>
or somesuch; seeman sed
or the wealth of information available on the web regarding use ofsed
.One thing to point out is the precise meaning of "carriage return" in the above; if you truly mean the single control character "carriage return", then the pattern above is correct. If you meant, more generally, CRLF (carriage return and a line feed, which is how line feeds are implemented under Windows), then you probably want to replace
\r\n
instead. Bare line feeds (newline) in Linux/Unix are\n
.Old School:
Once more a solution... Because there's always one more:
It's nice because it's in place and works in every flavor of unix/linux I've worked with.
I've used python for it, here my code;