7

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?

Community
  • 1
  • 1
Hongxu Chen
  • 5,240
  • 2
  • 45
  • 85

3 Answers3

12

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.

fge
  • 119,121
  • 33
  • 254
  • 329
  • That is most bizarre, this command has always worked for me. Care to tell how you attempt to use it? – fge Dec 29 '12 at 10:23
  • The `build_test.sh` is edited firstly using `leafpad`(not from Windows or dos OS). I just use `dos2unix build_test.sh build_test_nocr.sh` and the `file build_test_nocr.sh` tells me that `build_test_nocr.sh: ASCII text, with CR line terminators` – Hongxu Chen Dec 29 '12 at 10:30
  • You said this is "not from Windows or dos OS" -- what OS is that then? – fge Dec 29 '12 at 11:05
  • OK, I made a very gross error in the syntax, in fact dos2unix modifies the file in place... Try and see the edited post – fge Dec 29 '12 at 11:19
  • You mean `dos2unix build_test.sh`? No effect. I updated the question and hope you can explain :-> – Hongxu Chen Dec 29 '12 at 11:24
  • Hmm, it looks like `\r`-only line terminators are old Mac style terminators. Can you try `mac2unix` instead? – fge Dec 29 '12 at 11:30
  • Yes, it works;thanks! BTW, would you please tell me about the odd result of `cat`,`more` or some more details, please? – Hongxu Chen Dec 29 '12 at 11:34
  • 1
    I don't know exactly, but I _think_ this has to do with the way the terminal itself treats newlines. As I see it, terminals will recognize a `\n` character as a newline and do the appropriate action on the display, whereas they will display `\r` as `^M`. Terminals (well, ttys) are a complicated matter... – fge Dec 29 '12 at 11:44
11

Also, if you work with vim, you can enforce UNIX line endings by executing

:set fileformat=unix
:w

or just add

set fileformat=unix

to your .vimrc file

jpmuc
  • 1,092
  • 14
  • 30
6

I finally figured out that I could use this command:

tr '^M' '\n' <build_test.sh >build_test_nocr.sh

where ^M is added by pressing Ctrl+v and Enter keys.Alternately, this has the same effect:

tr '\r' '\n' <build_test.sh >build_test_nocr.sh
Hongxu Chen
  • 5,240
  • 2
  • 45
  • 85