2012-12-29 40 views
5

首先,我會說我已經閱讀this post但我仍然有問題的CR line terminators刪除CR行終止符

有一個叫build_test.sh文件,我在leafpad編輯,並可以就在Vim顯示:

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

但是:

  1. 使用cat build_test.sh它輸出什麼
  2. 使用more build_test.sh它輸出:cd - install/llvm-obj/tools/TEST_TOOL/Y_TOOL/$1 test.cpp
  3. 使用less build_test.sh它輸出:cp ~/moonbox/llvm-2.9/lib/Transforms/MY_TOOL/$1 test.cpp^Mcd ~/moonbox/llvm-obj/tools/TEST_TOOL/^Mmake^Mmake install^Mcd -

file build_test.sh結果是:

build_test.sh: ASCII text, with CR line terminators 

this post,該^M不再存在但是沒有更多換行符:-(
file build_test_no_cr.sh的結果是n ow:

build_test_nocr.sh: ASCII text, with no line terminators 

解決方案可見here

但是,我仍然想爲什麼cat什麼也沒有顯示,more顯示如此奇怪的結果。此外,爲什麼在這種情況下Vim的dos2unixset fileformat=unix失敗

PS:我想這也許是我的編輯器(Vim的leafpad?)只\r\n的換行符產生。它怎麼會是這樣?

回答

10

簡單\r換行符的終止符是「舊的Mac」行結束符,奇怪的是2012+編輯器甚至生成這樣的行終止符的文件......無論如何,您可以使用mac2unix命令,它是dos2unix分佈:

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

這個命令不會包含已經預期行終止符的文件,這是一種獎勵。而反向(unix2mac)也存在。

請注意,mac2unixdos2unix -c mac相同。

+0

不幸,我沒有任何效果嘗試;-( –

+0

這是最離奇的,這個命令一直爲我工作,請注意如何嘗試使用它? – fge

+0

'build_test.sh'首先使用'leafpad'編輯(不是來回m Windows或dos操作系統)。我只是使用'dos2unix build_test.sh build_test_nocr.sh'和'build_test_nocr.sh'文件告訴我'build_test_nocr.sh:ASCII文本,帶有CR行結束符' –

6

另外,如果你用vim的工作,你可以通過執行

:set fileformat=unix 
:w 

或執行UNIX行尾只需添加

set fileformat=unix 

你。vimrc文件

+1

它不起作用:-( –

4

我終於想通了,我可以使用這個命令:

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

其中^M是按Ctrl+vEnter keys.Alternately增加,這有同樣的效果:

tr '\r' '\n' <build_test.sh >build_test_nocr.sh