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
I'm going to assume you mean carriage returns (CR,
"\r"
,0x0d
) at the ends of lines rather than just blindly within a file (you may have them in the middle of strings for all I know). Using this test file with a CR at the end of the first line only:dos2unix
is the way to go if it's installed on your system:If for some reason
dos2unix
is not available to you, thensed
will do it:If for some reason
sed
is not available to you, thened
will do it, in a complicated way:If you don't have any of those tools installed on your box, you've got bigger problems than trying to convert files :-)
There's a utility called dos2unix that exists on many systems, and can be easily installed on most.
If you are a Vi user, you may open the file and remove the carriage return with:
or with
Note that you should type ^M by pressing ctrl-v and then ctrl-m.
try this to convert dos file into unix file:
If you're using an OS (like OS X) that doesn't have the
dos2unix
command but does have a Python interpreter (version 2.5+), this command is equivalent to thedos2unix
command:This handles both named files on the command line as well as pipes and redirects, just like
dos2unix
. If you add this line to your ~/.bashrc file (or equivalent profile file for other shells):... the next time you log in (or run
source ~/.bashrc
in the current session) you will be able to use thedos2unix
name on the command line in the same manner as in the other examples.If you are running an X environment and have a proper editor (visual studio code), then I would follow the reccomendation:
Visual Studio Code: How to show line endings
Just go to the bottom right corner of your screen, visual studio code will show you both the file encoding and the end of line convention followed by the file, an just with a simple click you can switch that around.
Just use visual code as your replacement for notepad++ on a linux environment and you are set to go.