问题
I have been using d2u
to convert line endings. After installing Puppy Linux I
noticed that it does not come with d2u
, but dos2unix
. Then I noticed that
Ubuntu is missing both by default.
What is another way to convert line endings?
回答1:
Some options:
Using tr
tr -d '\15\32' < windows.txt > unix.txt
OR
tr -d '\r' < windows.txt > unix.txt
Using perl
perl -p -e 's/\r$//' < windows.txt > unix.txt
Using sed
sed 's/^M$//' windows.txt > unix.txt
OR
sed 's/\r$//' windows.txt > unix.txt
To obtain ^M
, you have to type CTRL-V
and then CTRL-M
.
回答2:
Doing this with POSIX is tricky:
POSIX Sed does not support
\r
or\15
. Even if it did, the in place option-i
is not POSIXPOSIX Awk does support
\r
and\15
, however the-i inplace
option is not POSIXd2u and dos2unix are not POSIX utilities, but ex is
POSIX ex does not support
\r
,\15
,\n
or\12
To remove carriage returns:
awk 'BEGIN{RS="^$";ORS="";getline;gsub("\r","");print>ARGV[1]}' file
To add carriage returns:
awk 'BEGIN{RS="^$";ORS="";getline;gsub("\n","\r&");print>ARGV[1]}' file
来源:https://stackoverflow.com/questions/16768776/convert-line-endings