iT邦幫忙

0

刪除txt file行尾空白

help27 2010-07-12 17:32:5930725 瀏覽

請教各位,用什麼工具或方式可將txt file中多行的行尾空白去除;
如下範例:
原始file
111111111111
222222222222
333333333333
希望變為
111111111111222222222222333333333333

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
12
shunyuan
iT邦研究生 1 級 ‧ 2010-07-12 17:45:54
最佳解答

UltraEdit 有一個選項,存檔時移除每行最後的空白。

這個軟體,很強,值得花錢買正版。

看更多先前的回應...收起先前的回應...
hiada iT邦新手 4 級 ‧ 2010-07-13 10:24:58 檢舉

推+1
我公司就有敗一套超好用的

shunyuan iT邦研究生 1 級 ‧ 2010-07-13 11:07:33 檢舉

UltraEdit 還有巨集功能,所以你做過一遍的動作,可以錄下來,下次就不用按按鈕了。喜歡

shunyuan iT邦研究生 1 級 ‧ 2010-07-13 11:08:10 檢舉

hiada提到:
我公司就有敗一套超好用的

不貴啊,還好吧。毆飛

sula3065408 iT邦研究生 1 級 ‧ 2010-07-14 19:10:07 檢舉

UltraEdit空白可以,\r\n好像不行吧~

16
kenkk
iT邦新手 3 級 ‧ 2010-07-12 17:46:51

我是用 ultraedit ,搜尋--取代 (勾選正規運算式)
尋找內容(N) 填入 "^p" (換行符號)
取代成為: 填入 "" (就是什麼都不填)
全部取代..就會是這樣了...

如果是常態或背景要用, 可以考慮 perl....他的字元操作也頂好用說

22
逮丸逮丸
iT邦大師 1 級 ‧ 2010-07-12 18:48:15

請參考這篇精彩的文章:
rmnl — remove new line characters with tr, awk, perl, sed or c/c++
直接列出用 *nix 上的工具就做出的例子。

以 sed 為例,假設檔案為 1.txt
參考 如何用"批次檔"過濾log中的資料 一文中,
有關 Sed for Windows 的安裝,
利用 sed 來完成:

<pre class="c" name="code">C:\>sed ":a;N;$!ba;s/\n//g" 1.txt
111111111111222222222222333333333333

其他工具完成,請參考該文的具體例子。

補一個該文沒有的:
在 vim 只要在命令模式下鍵入:

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

也可以達到同樣的效果。

4
aygw
iT邦新手 3 級 ‧ 2010-07-14 00:19:12

-MS word can do the trick too
1.
open the txt file in MSword
2.
CTL-H to replace
3.
^P is the paragraph mark (enter in the "Find What")
4.
replace with blank (enter nothing in the "replace with")
5.
Click "replace all"

This will do it!

4
zcm
iT邦研究生 3 級 ‧ 2010-07-14 11:41:31

寫個vbscript也可以呀!

Const ForReading = 1
Const ForWriting = 2

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("TEST.TXT", ForReading)

Do Until objFile.AtEndOfStream
strLine = objFile.Readline
strLine = strLine
If not objFile.AtEndOfStream Then
strNewContents = strNewContents & trim(strLine)
ELSE
strNewContents = strNewContents & trim(strLine)
End If
Loop

objFile.Close
Set objFile = objFSO.OpenTextFile("TEST.TXT", ForWriting)
objFile.Write strNewContents
objFile.Close

Wscript.Echo "Done."

以上這段就符合您的需求了解!
打----中間那段copy起來, 打開記書本貼上, 存成trimall.vbs, 放到test.txt同一目錄, 再執行trimall.vbs即可!

sula3065408 iT邦研究生 1 級 ‧ 2010-07-14 19:11:59 檢舉

好長唷~Sed好短唷

其實每個程式語言都可以做到同樣的功能,
但大多都是要從頭造輪子開始,
是比較辛苦,但也鍛鍊了解決問題的能力。
所以目的看是為了快速有效解決問題?
或是提升程式的功力?
其實是各有優缺。

6
huangsb
iT邦好手 1 級 ‧ 2010-07-18 23:25:02

使用Notepad++將換行符號直接取代成空字串即可
步驟如下:

  1. 按下「Ctrl+H」。
  2. 搜尋目標輸入「\r\n」,取代成請保留空白。
  3. 搜尋模式選擇「增強模式(\n, \r, \t, \0, \x...)。
  4. 按下「找下一個」,然後逐一按下「取代」;或直接按下「全部取代」。

我要發表回答

立即登入回答