Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove CR line terminators

Firstly I would say that I have read this post however I still have problems for the CR line terminators.

There is a file called build_test.sh, I edited in leafpad and it can be displayed right in Vim:

cp ~/moonbox/llvm-2.9/lib/Transforms/MY_TOOL/$1 test.cpp 
cd ~/moonbox/llvm-obj/tools/TEST_TOOL/
make
make install
cd -

However:

  1. Using cat build_test.sh it outputs nothing.
  2. Using more build_test.sh it outputs:cd - install/llvm-obj/tools/TEST_TOOL/Y_TOOL/$1 test.cpp
  3. Using less build_test.sh it outputs: cp ~/moonbox/llvm-2.9/lib/Transforms/MY_TOOL/$1 test.cpp^Mcd ~/moonbox/llvm-obj/tools/TEST_TOOL/^Mmake^Mmake install^Mcd -

The result of file build_test.sh is:

build_test.sh: ASCII text, with CR line terminators

Following this post, the ^M no longer exists however there is no more line break :-(
The result of file build_test_no_cr.sh is now:

build_test_nocr.sh: ASCII text, with no line terminators

The solution can be seen here.

However I still would like why cat displays nothing and more displays so odd result. In addition why dos2unix and set fileformat=unix in Vim fails for this case.

ps: I guess that maybe my editor(Vim or leafpad?) generates only \r rather \n for the newline. How can it be so?

like image 728
Hongxu Chen Avatar asked Dec 29 '12 09:12

Hongxu Chen


1 Answers

Simple \r terminators for newlines are "old Mac" line terminators, it is strange that an editor in 2012+ even generates files with such line terminators... Anyway, you can use the mac2unix command, which is part of the dos2unix distribution:

# Edits thefile inline
mac2unix thefile
# Takes origfile as an input, outputs to dstfile
mac2unix -n origfile dstfile

This command will not munge files which have already expected line terminators, which is a bonus. And the reverse (unix2mac) also exists.

Note that mac2unix is the same as dos2unix -c mac.

like image 147
fge Avatar answered Nov 23 '22 18:11

fge