iT邦幫忙

0

【如何改變文字檔的斷行??】

  • 分享至 

  • twitterImage

文字檔是我在ubuntu執行的log
我發現有的斷行符號是完整的
CR LF
有的只有 CR

請問我要如何讓文字檔中的 CR
變成完整的
CR LF 呢
感謝大家

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

8
逮丸逮丸
iT邦大師 1 級 ‧ 2012-03-16 23:52:04
最佳解答

用vim下指令試試。
\r 為 CR、\n 為LF
把單行只有\r的改成\r\n

<pre class="c" name="code">:1,$ s/^\r$/\r\n/g

把行末只有\r的,改為\r\n
<pre class="c" name="code">:1,$ s/\r$/\r\n/g

看更多先前的回應...收起先前的回應...
andyto202 iT邦研究生 4 級 ‧ 2012-03-17 00:35:50 檢舉

:1,$ s/^\r$/\r\n/g

請問要怎麼下啊
我開啟檔案後
只有 CR 的
裡面會顯示 ^M

為何unix2dos未成功,
是因為 unix2dos 只把 \n 轉成 \r\n
並不會把 \r 轉成 \r\n。
試驗一下在notepad++上分別以windows、unix格式存成兩檔,
上傳到linux上:
內容分別為:

&lt;pre class="c" name="code">$ cat win.txt
win1
win2$ cat unix.txt
unix1
unix2$
#觀察裡面的字碼
$ od -a win.txt
0000000   w   i   n   1  cr  nl   w   i   n   2
0000012
$ od -a unix.txt
0000000   u   n   i   x   1  nl   u   n   i   x   2
0000013
#一般轉\n轉成\r\n
$ unix2dos unix.txt
unix2dos: converting file unix.txt to DOS format ...
$ od -a unix.txt
0000000   u   n   i   x   1  cr  nl   u   n   i   x   2
0000014
#利用 ruby 產生一個 以\r 的兩行
$ cat put.rb
printf "line1\r"
printf "line2"
$ ruby put.rb > line.txt
$ cat line.txt
line2$
#這是在顯示上 line2 蓋過 line1
$ od -a line.txt
0000000   l   i   n   e   1  cr   l   i   n   e   2
0000013
#用unix2dos就未處理到:
$ unix2dos line.txt
unix2dos: converting file line.txt to DOS format ...
$ od -a line.txt
0000000   l   i   n   e   1  cr   l   i   n   e   2
0000013

再仔細測試,
用 tr 把 \r 取代成 \n沒問題:

&lt;pre class="c" name="code">$ tr '\r' '\n' &lt; line.txt > line.2
$ od -a line.2
0000000   l   i   n   e   1  nl   l   i   n   e   2
0000013
# 但想要把 \r 取代成 \r\n 卻不成:
$ tr '\r' '\r\n' &lt; line.txt > line.3
$ od -a line.3
0000000   l   i   n   e   1  cr   l   i   n   e   2
0000013

所以理想的處理方式,
是先用 $ tr '\r' '\n' < line.txt > line.2 的指令,
把所有 \r 轉成 \n 後,再用 unix2dos 把所有 \n 轉成 \r\n 就會成功。

也得修正原來說用 vim 以 :1,$ s/\r/\r\n/g 的方式,
因為取代出來的結果也有點怪:
<pre class="c" name="code">$ od -a line.txt
0000000 l i n e 1 nl nul l i n e 2 nl
0000015

所以暫不要用vim處理,用tr + unix2dos可順利解決。

有關 cr lf 怎麼以 vim 處理,
這篇 File format - Vim Tips Wiki 說明的較詳細。

文中才知道,原來有個
flip: Newline conversion between Unix, Macintosh and MS-DOS ASCII files
的工具,也把 CR 的格式處理也考慮進去。

而從 Newline - Wikipedia, the free encyclopedia 提供不同工具的解決方式,可供參考。

wiseguy iT邦超人 1 級 ‧ 2012-03-17 15:55:08 檢舉

我給樓主的那行 php 指令就不管後面是 \r 還是 \n,通通都會換成 \r\n,只是不知道他有沒有試。

6
wiseguy
iT邦超人 1 級 ‧ 2012-03-16 22:03:37

既然是在 ubuntu,那就下個《unix2dos 文字檔》就轉過去了。

看更多先前的回應...收起先前的回應...
andyto202 iT邦研究生 4 級 ‧ 2012-03-16 22:49:59 檢舉

iT邦幫忙MVPwiseguy提到:
unix2dos

wiseguy兄
我用 todos 轉出來,一樣只有 CR
有可能用 php 改變嗎??

wiseguy iT邦超人 1 級 ‧ 2012-03-17 00:07:55 檢舉

你說的 todos 不是我說的 unix2dos 喔
建議你還是安裝一下 unix2dos 來轉轉看
php 是可以改,這樣下就行了 (底下是一整行命令):
php -r 'foreach(file("原檔案") as $a) $b[]=rtrim($a); file_put_contents("新檔案", implode("\r\n", $b));'

andyto202 iT邦研究生 4 級 ‧ 2012-03-17 00:28:41 檢舉

wiseguy提到:
php -r 'foreach(file("原檔案") as $a) $b[]=rtrim($a); file_put_contents("新檔案", implode("\r\n", $b));'

wiseguy兄,您的指令執行也一樣
您有 mail 嗎
我寄給你,你就知道了

andyto202 iT邦研究生 4 級 ‧ 2012-03-17 00:43:45 檢舉

iT邦幫忙MVPwiseguy提到:
unix2dos

unix2dos我試了
不行

andyto202 iT邦研究生 4 級 ‧ 2012-03-17 00:44:18 檢舉

寄錯了

wiseguy iT邦超人 1 級 ‧ 2012-03-17 00:44:28 檢舉

哪有那麼神奇?
unix2dos 我都用十幾年了,這麼簡單的轉換行尾動作,有這麼困難?
上面那個 php 指令我也剛剛才試過,都是 OK 順利轉換過去的。
你用就不行?這麼玄?轉換的時候有出現什麼訊息嗎?

wiseguy iT邦超人 1 級 ‧ 2012-03-17 00:48:58 檢舉

只要打《unix2dos PCPC-207_2012-03-16.txt》就好了,它會直接改原檔。

我要發表回答

立即登入回答