Return symbol: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
m (→如何尋找與取代換行符號) |
||
| (33 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
如何看到換行符號、如何從文章中取代換行符號 (英文:'''Return symbol''', line terminators, line endings, newline, end of line (EOL), line feed (LF) or line break)。 | |||
* On {{Win}}: {{kbd | key=<nowiki>CRLF</nowiki>}} = {{kbd | key=<nowiki>\r\n</nowiki>}} | * On {{Win}}: {{kbd | key=<nowiki>CRLF</nowiki>}} = {{kbd | key=<nowiki>\r\n</nowiki>}} | ||
* On Unix, {{Linux}} & recent versions of {{Mac}}: {{kbd | key=<nowiki>LF</nowiki>}} = {{kbd | key=<nowiki>\n</nowiki>}} | * On Unix, {{Linux}} & recent versions of {{Mac}}: {{kbd | key=<nowiki>LF</nowiki>}} = {{kbd | key=<nowiki>\n</nowiki>}} | ||
* On classic versions of {{Mac}}<ref>[https://en.wikipedia.org/wiki/Classic_Mac_OS Classic Mac OS - Wikipedia]</ref><ref>[https://en.wikipedia.org/wiki/Newline Newline - Wikipedia]</ref> e.g. Mac OS 9: {{kbd | key=<nowiki>CR</nowiki>}} = {{kbd | key=<nowiki>\r</nowiki>}} | * On classic versions of {{Mac}}<ref>[https://en.wikipedia.org/wiki/Classic_Mac_OS Classic Mac OS - Wikipedia]</ref><ref>[https://en.wikipedia.org/wiki/Newline Newline - Wikipedia]</ref> e.g. Mac OS 9: {{kbd | key=<nowiki>CR</nowiki>}} = {{kbd | key=<nowiki>\r</nowiki>}} | ||
== | == 如何使用編輯軟體,看到換行符號 == | ||
查看每一行使用的換行符號 | 查看每一行使用的換行符號 | ||
* [https://notepad-plus-plus.org/zh/ Notepad++] On {{Win}} 選單 → 檢視 → 特殊字元 → 顯示行尾字元 (EOL) | * [https://notepad-plus-plus.org/zh/ Notepad++] On {{Win}} 選單 → 檢視 → 特殊字元 → 顯示行尾字元 (EOL) | ||
* [https://github.com/facelessuser/RawLineEdit RawLineEdit] for [https://www.sublimetext.com/ Sublime text] | * [https://github.com/facelessuser/RawLineEdit RawLineEdit] for [https://www.sublimetext.com/ Sublime text] | ||
** 換行符號是 {{kbd | key=<nowiki>CRLF</nowiki>}} = {{kbd | key=<nowiki>\r\n</nowiki>}} | ** 換行符號是 {{kbd | key=<nowiki>CRLF</nowiki>}} = {{kbd | key=<nowiki>\r\n</nowiki>}} 每行最後顯示的是 {{kbd | key=<nowiki><0x0d>¬</nowiki>}} | ||
** 換行符號是 {{kbd | key=<nowiki>LF</nowiki>}} = {{kbd | key=<nowiki>\n</nowiki>}} | ** 換行符號是 {{kbd | key=<nowiki>LF</nowiki>}} = {{kbd | key=<nowiki>\n</nowiki>}} 每行最後顯示的是 {{kbd | key=<nowiki>¬</nowiki>}} 象形符號 (glyph) | ||
** 換行符號是 {{kbd | key=<nowiki>CR</nowiki>}} = {{kbd | key=<nowiki>\r</nowiki>}} | ** 換行符號是 {{kbd | key=<nowiki>CR</nowiki>}} = {{kbd | key=<nowiki>\r</nowiki>}} 每行最後顯示的是 {{kbd | key=<nowiki><0x0d></nowiki>}} | ||
* BASH [http://www.linfo.org/cat.html cat] command e.g. {{kbd | key=<nowiki>cat -e <filename></nowiki>}} | * BASH [http://www.linfo.org/cat.html cat] command e.g. {{kbd | key=<nowiki>cat -e <filename></nowiki>}} | ||
** 換行符號是 {{kbd | key=<nowiki>CRLF</nowiki>}} = {{kbd | key=<nowiki>\r\n</nowiki>}} | ** 換行符號是 {{kbd | key=<nowiki>CRLF</nowiki>}} = {{kbd | key=<nowiki>\r\n</nowiki>}} 每行最後顯示的是 {{kbd | key=<nowiki>^M$</nowiki>}} | ||
** 換行符號是 {{kbd | key=<nowiki>LF</nowiki>}} = {{kbd | key=<nowiki>\n</nowiki>}} | ** 換行符號是 {{kbd | key=<nowiki>LF</nowiki>}} = {{kbd | key=<nowiki>\n</nowiki>}} 每行最後顯示的是 {{kbd | key=<nowiki>$</nowiki>}} | ||
** 換行符號是 {{kbd | key=<nowiki>CR</nowiki>}} = {{kbd | key=<nowiki>\r</nowiki>}} | ** 換行符號是 {{kbd | key=<nowiki>CR</nowiki>}} = {{kbd | key=<nowiki>\r</nowiki>}} 每行最後顯示的是 {{kbd | key=<nowiki>^M</nowiki>}} | ||
查看檔案使用的換行符號 | 查看檔案使用的換行符號 | ||
| Line 63: | Line 63: | ||
</table> | </table> | ||
== 不同換行符號 | == 不同換行符號 在不同作業系統的編輯器看到的文件狀況 == | ||
<table border="1" class="wikitable" > | <table border="1" class="wikitable" > | ||
| Line 100: | Line 100: | ||
</table> | </table> | ||
== | == 計算換行符號的數量 == | ||
<pre> | |||
## 計算 \n (不包含 \r\n) 換行符號的數量 | |||
% perl -ne '$count++ if /[^\r]\n/; END{print "純 LF 數量: $count\n"}' YOUR_FILE.txt | |||
## 計算 \r\n 換行符號的數量 | |||
% perl -ne '$count++ if /\r\n/; END{print "CRLF 數量: $count\n"}' YOUR_FILE.txt | |||
</pre> | |||
== 如何尋找與取代換行符號 == | |||
=== 文字編輯軟體如何取代換行符號 === | |||
* 支援 [[Regular expression]] 的文字編輯軟體,例: [[Regular expression#將Email清單,轉成Email軟體可以使用的寄信名單 (取代換行符號為逗號) | 將Email清單,轉成Email軟體可以使用的寄信名單 (取代換行符號為逗號)]] | * 支援 [[Regular expression]] 的文字編輯軟體,例: [[Regular expression#將Email清單,轉成Email軟體可以使用的寄信名單 (取代換行符號為逗號) | 將Email清單,轉成Email軟體可以使用的寄信名單 (取代換行符號為逗號)]] | ||
* | |||
=== MySQL 如何尋找換行符號 === | |||
<pre> | |||
SELECT content, | |||
CASE | |||
WHEN content LIKE CONCAT('%', CHAR(13), CHAR(10), '%') THEN 'CRLF (\\r\\n)' | |||
WHEN content LIKE CONCAT('%', CHAR(10), '%') THEN 'LF (\\n)' | |||
WHEN content LIKE CONCAT('%', CHAR(13), '%') THEN 'CR (\\r)' | |||
ELSE 'No newlines' | |||
END as newline_type | |||
FROM my_table | |||
WHERE content REGEXP CONCAT('[', CHAR(10), CHAR(13), ']'); | |||
</pre> | |||
=== MySQL 如何取代換行符號 === | |||
<ref> [https://stackoverflow.com/questions/13273343/replace-new-line-character-in-mysql-not-working sql - REPLACE new line character in MYSql not working - Stack Overflow] </ref><ref>[https://stackoverflow.com/questions/28368785/mysql-trim-both-whitespace-and-newline-characters MySQL: trim *both* whitespace and newline characters - Stack Overflow]</ref> | |||
<pre> | |||
UPDATE my_table | |||
SET content = REPLACE( | |||
REPLACE( | |||
REPLACE(content, CONCAT(CHAR(13), CHAR(10)), ''), | |||
CHAR(10), '' | |||
), | |||
CHAR(13), '' | |||
) | |||
WHERE content REGEXP CONCAT('[', CHAR(10), CHAR(13), ']'); | |||
-- or | |||
UPDATE my_table | |||
SET `content` = REPLACE(REPLACE(REPLACE(`my_table`,'\r\n',''),'\n',''),'\r','') | |||
; | |||
</pre> | |||
=== Excel 或 Google Sheet 如何尋找換行符號 === | |||
判斷是哪種換行符號: | |||
<pre> | |||
=IF(ISNUMBER(SEARCH(CHAR(13)&CHAR(10), A1)), "CRLF (\r\n)", | |||
IF(ISNUMBER(SEARCH(CHAR(13), A1)), "CR (\r)", | |||
IF(ISNUMBER(SEARCH(CHAR(10), A1)), "LF (\n)", | |||
"無換行符號"))) | |||
</pre> | |||
=== Excel 或 Google Sheet 如何取代換行符號 === | |||
For Microsoft Excel or Google Sheet | |||
* {{kbd | key=<nowiki>=CLEAN(A1)</nowiki>}} Use the CLEAN function to "remove the first 32 nonprinting characters in the 7-bit ASCII code (values 0 through 31) from the text"<ref>[https://support.microsoft.com/en-us/office/clean-function-26f3d7c5-475f-4a9c-90e5-4b8ba987ba41 CLEAN function]</ref>. Meanwhile, the return symbol will be removed from the text. | |||
* {{kbd | key=<nowiki>=TRIM(SUBSTITUTE(SUBSTITUTE(A1, CHAR(13),""), CHAR(10),", ")</nowiki>}} replace the return symbols with spaces in the cell located at {{kbd | key=A1}} <ref>[https://www.ablebits.com/office-addins-blog/2013/12/03/remove-carriage-returns-excel/ 3 ways to remove carriage returns in Excel: formulas, VBA macro, find&replace dialog] {{access | date = 2018-05-24}}</ref><ref>[https://en.wikipedia.org/wiki/ASCII ASCII - Wikipedia]</ref>. | |||
函數說明 | |||
* {{kbd | key=CONCAT(CHAR(13), CHAR(10))}} means "CRLF"({{kbd | key=<nowiki>\r\n</nowiki>}}) Windows 標準換行符號 | |||
* {{kbd | key=CHAR(13)}} means "Carriage return"(CR, {{kbd | key=<nowiki>\r</nowiki>}}) 舊版 Mac 換行符號 | |||
* {{kbd | key=CHAR(10)}} means "Line feed" (LF, {{kbd | key=<nowiki>\n</nowiki>}}) Unix/Linux 換行符號 | |||
For Google Sheet | |||
* {{kbd | key=<nowiki>=REGEXREPLACE(A1, "[\r\n]+", " ")</nowiki>}} replaces return symbols with spaces in cell {{kbd | key=A1}} in Google Sheets | |||
函數說明 | |||
* {{kbd | key=<nowiki>[\r\n]</nowiki>}} 符合任意類型的換行符號 | |||
* {{kbd | key=<nowiki>[\r\n]+</nowiki>}} 符合任意類型的換行符號,出現一次或連續出現 | |||
=== Python 如何取代換行符號 === | |||
* [https://stackoverflow.com/questions/275018/how-can-i-remove-a-trailing-newline-in-python How can I remove a trailing newline in Python? - Stack Overflow] {{access | date = 2019-06-14}} | |||
== Related articles == | == Related articles == | ||
* [https://en.wikipedia.org/wiki/Carriage_return Carriage return - Wikipedia] | * [https://en.wikipedia.org/wiki/Carriage_return Carriage return - Wikipedia] | ||
* [https://en.wikipedia.org/wiki/Newline Newline - Wikipedia] | |||
* [https://zh.wikipedia.org/zh-tw/%E6%8F%9B%E8%A1%8C 換行 - 维基百科,自由的百科全书] | * [https://zh.wikipedia.org/zh-tw/%E6%8F%9B%E8%A1%8C 換行 - 维基百科,自由的百科全书] | ||
* [https://zh.wikipedia.org/wiki/%E5%9B%9E%E8%BB%8A%E9%8D%B5 回車鍵 - 維基百科,自由的百科全書] | * [https://zh.wikipedia.org/wiki/%E5%9B%9E%E8%BB%8A%E9%8D%B5 回車鍵 - 維基百科,自由的百科全書] | ||
| Line 112: | Line 190: | ||
* [https://confluence.qps.nl/fledermaus/questions-answers/other/differences-in-end-of-line-characters-mac-windows-and-linux Differences in end of line characters Mac, Windows, and Linux] | * [https://confluence.qps.nl/fledermaus/questions-answers/other/differences-in-end-of-line-characters-mac-windows-and-linux Differences in end of line characters Mac, Windows, and Linux] | ||
* [https://stackoverflow.com/questions/3059091/how-to-remove-carriage-returns-from-output-of-string php - How to remove carriage returns from output of string? - Stack Overflow] | * [https://stackoverflow.com/questions/3059091/how-to-remove-carriage-returns-from-output-of-string php - How to remove carriage returns from output of string? - Stack Overflow] | ||
* Url encoding of {{kbd | key=<nowiki>\r\n</nowiki>}} is {{kbd | key=<nowiki>%0D%0A</nowiki>}}. More details on [https://www.w3schools.com/tags/ref_urlencode.asp HTML URL Encoding Reference] | |||
References | == References == | ||
<references /> | <references /> | ||
[[Category:Programming]] [[Category:Data Science]] [[Category: | [[Category: Programming]] | ||
[[Category: Data Science]] | |||
[[Category: String manipulation]] | |||
[[Category: Revised with LLMs]] | |||
Latest revision as of 16:32, 28 November 2025
如何看到換行符號、如何從文章中取代換行符號 (英文:Return symbol, line terminators, line endings, newline, end of line (EOL), line feed (LF) or line break)。
- On Win
: CRLF = \r\n - On Unix, Linux
& recent versions of macOS
: LF = \n - On classic versions of macOS
[1][2] e.g. Mac OS 9: CR = \r
如何使用編輯軟體,看到換行符號[edit]
查看每一行使用的換行符號
- Notepad++ On Win
選單 → 檢視 → 特殊字元 → 顯示行尾字元 (EOL) - RawLineEdit for Sublime text
- 換行符號是 CRLF = \r\n 每行最後顯示的是 <0x0d>¬
- 換行符號是 LF = \n 每行最後顯示的是 ¬ 象形符號 (glyph)
- 換行符號是 CR = \r 每行最後顯示的是 <0x0d>
- BASH cat command e.g. cat -e <filename>
- 換行符號是 CRLF = \r\n 每行最後顯示的是 ^M$
- 換行符號是 LF = \n 每行最後顯示的是 $
- 換行符號是 CR = \r 每行最後顯示的是 ^M
查看檔案使用的換行符號
- atom/line-ending-selector: Switch between Unix and Window style line endings. for Atom on Win
, macOS
& Linux
- file (command): file filename.txt on Linux
, macOS
[3] & Cygwin on Win
| 檔案內容 | File command 偵測結果 |
|---|---|
| 換行符號是 CRLF = \r\n | UTF-8 Unicode text, with CRLF line terminators |
| 換行符號是 LF = \n | UTF-8 Unicode text |
| 換行符號是 CR = \r | UTF-8 Unicode text, with CR line terminators |
| 特例: 換行符號夾雜 \r\n 和 \r | UTF-8 Unicode text, with CRLF, CR line terminators |
| 特例: 換行符號夾雜 \r\n 和 \n | UTF-8 Unicode text, with CRLF, LF line terminators |
| 特例: 換行符號夾雜 \n 和 \r | UTF-8 Unicode text, with CR, LF line terminators |
| 特例: 無內容的空檔案 | empty |
不同換行符號 在不同作業系統的編輯器看到的文件狀況[edit]
| 檔案內容 | Windows 10 內建「記事本」 | Windows 10 免費編輯器 Notepad++ v. 7.5.9 | Mac 內建「文字編輯.app」 | Mac 編輯器 Sublime Text v. 3.2 |
|---|---|---|---|---|
| 換行符號是 CRLF = \r\n | ok | ok | ok | ok |
| 換行符號是 LF = \n | ok | ok | ok | |
| 換行符號是 CR = \r | ok | ok | ok |
計算換行符號的數量[edit]
## 計算 \n (不包含 \r\n) 換行符號的數量
% perl -ne '$count++ if /[^\r]\n/; END{print "純 LF 數量: $count\n"}' YOUR_FILE.txt
## 計算 \r\n 換行符號的數量
% perl -ne '$count++ if /\r\n/; END{print "CRLF 數量: $count\n"}' YOUR_FILE.txt
如何尋找與取代換行符號[edit]
文字編輯軟體如何取代換行符號[edit]
- 支援 Regular expression 的文字編輯軟體,例: 將Email清單,轉成Email軟體可以使用的寄信名單 (取代換行符號為逗號)
MySQL 如何尋找換行符號[edit]
SELECT content,
CASE
WHEN content LIKE CONCAT('%', CHAR(13), CHAR(10), '%') THEN 'CRLF (\\r\\n)'
WHEN content LIKE CONCAT('%', CHAR(10), '%') THEN 'LF (\\n)'
WHEN content LIKE CONCAT('%', CHAR(13), '%') THEN 'CR (\\r)'
ELSE 'No newlines'
END as newline_type
FROM my_table
WHERE content REGEXP CONCAT('[', CHAR(10), CHAR(13), ']');
MySQL 如何取代換行符號[edit]
UPDATE my_table
SET content = REPLACE(
REPLACE(
REPLACE(content, CONCAT(CHAR(13), CHAR(10)), ''),
CHAR(10), ''
),
CHAR(13), ''
)
WHERE content REGEXP CONCAT('[', CHAR(10), CHAR(13), ']');
-- or
UPDATE my_table
SET `content` = REPLACE(REPLACE(REPLACE(`my_table`,'\r\n',''),'\n',''),'\r','')
;
Excel 或 Google Sheet 如何尋找換行符號[edit]
判斷是哪種換行符號:
=IF(ISNUMBER(SEARCH(CHAR(13)&CHAR(10), A1)), "CRLF (\r\n)",
IF(ISNUMBER(SEARCH(CHAR(13), A1)), "CR (\r)",
IF(ISNUMBER(SEARCH(CHAR(10), A1)), "LF (\n)",
"無換行符號")))
Excel 或 Google Sheet 如何取代換行符號[edit]
For Microsoft Excel or Google Sheet
- =CLEAN(A1) Use the CLEAN function to "remove the first 32 nonprinting characters in the 7-bit ASCII code (values 0 through 31) from the text"[6]. Meanwhile, the return symbol will be removed from the text.
- =TRIM(SUBSTITUTE(SUBSTITUTE(A1, CHAR(13),""), CHAR(10),", ") replace the return symbols with spaces in the cell located at A1 [7][8].
函數說明
- CONCAT(CHAR(13), CHAR(10)) means "CRLF"(\r\n) Windows 標準換行符號
- CHAR(13) means "Carriage return"(CR, \r) 舊版 Mac 換行符號
- CHAR(10) means "Line feed" (LF, \n) Unix/Linux 換行符號
For Google Sheet
- =REGEXREPLACE(A1, "[\r\n]+", " ") replaces return symbols with spaces in cell A1 in Google Sheets
函數說明
- [\r\n] 符合任意類型的換行符號
- [\r\n]+ 符合任意類型的換行符號,出現一次或連續出現
Python 如何取代換行符號[edit]
- How can I remove a trailing newline in Python? - Stack Overflow [Last visited: 2019-06-14]
Related articles[edit]
- Carriage return - Wikipedia
- Newline - Wikipedia
- 換行 - 维基百科,自由的百科全书
- 回車鍵 - 維基百科,自由的百科全書
- 陳雲濤的部落格: 筆記 CRLF跟LF之區別 --- 隱形的 bug:「CRLF才是真正的打字機運作方式,你可以想像:打字機打到最右邊,要先return到最左邊之後,再往下一行,所以是CRLF ... ...」
- Differences in end of line characters Mac, Windows, and Linux
- php - How to remove carriage returns from output of string? - Stack Overflow
- Url encoding of \r\n is %0D%0A. More details on HTML URL Encoding Reference
References[edit]
- ↑ Classic Mac OS - Wikipedia
- ↑ Newline - Wikipedia
- ↑ file(1) Mac OS X Manual Page
- ↑ sql - REPLACE new line character in MYSql not working - Stack Overflow
- ↑ MySQL: trim *both* whitespace and newline characters - Stack Overflow
- ↑ CLEAN function
- ↑ 3 ways to remove carriage returns in Excel: formulas, VBA macro, find&replace dialog [Last visited: 2018-05-24]
- ↑ ASCII - Wikipedia