Linux: 转换Windows换行为Unix换行

0. check

cat -A file.txt

1. dos2unix

sudo apt-get install dos2unix
dos2unix file.txt

2. sed

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

sed -i $'s/\r$//' file.txt     # DOS to Unix
sed -i $'s/$/\r/' file.txt     # Unix to DOS

sed -i 's/^M$//' file.txt     # DOS to Unix
sed -i 's/$/^M/' file.txt     # Unix to DOS

3. tr

tr -d "\r" < file
tr -d '\015' <DOS-file >UNIX-file

4. vim

vim file.txt -c "set ff=unix" -c ":wq"

5. awk

awk '{ sub("\r$", ""); print }' dos.txt > unix.txt

6. perl

perl -pe 's/\r$//' < dos.txt > unix.txt

7. ex

ex -bsc '%!awk "{sub(/\r/,\"\")}1"' -cx file