A carriage return linefeed (CRLF) is a special sequence of characters, used by DOS/Windows, to signify the end of a line of text. However, in *nix, only a linefeed (LF) is required to signify a line ending.

test.CRLF.txt

12345CRLF

12345LF

Delete/Replace a Carriage Return (CR) with sed

There are a few ways to remove or replace a carriage return with sed.

    sed 's/.$//' test.CRLF.txt > test1.txt

12345LF
1234LF

    sed 's/\x0D$//' test.CRLF.txt > test2.txt

12345LF
12345LF

    sed 's/\r//' test.CRLF.txt > test3.txt

12345LF
12345LF

The first method assumes all lines end with CRLF, while the second and third method depend on the version of sed.

Delete/Replace a Linefeed (LF) with sed

By default, sed strips the newline as the line is placed into the pattern space. However, the workaround is to read the whole file into a loop first.

sed ':a;N;$!ba;s/\n/<text>/g'

where

:a       - create a label 'a'
N        - append the next line to the pattern space
$!       - if not the last line
ba       - branch (go to) label 'a'
s        - substitute
/\n/     - regex for new line
/<text>/ - with text "<text>"
g        - global match (as many times as it can)

Example: Delete Newlines (CRLF) with sed

Delete the carriage return first, and then delete the linefeed.

sed -i 's/\r//g' example.txt
    sed example delete newline carriage return.png
    sed -i ':a;N;$!ba;s/\n/<text>/g' example.txt
    sed example delete newline linefeed.png

tr: A Simpler Way to Replace or Delete a Newline (CRLF)

The tr utility is a preferred and simpler method for replacing end of line characters as the pattern buffer is not limited like in sed.

tr '\r\n' '<replace with text>' < input_file > output_file

or to delete the newline (CRLF),

tr -d '\r\n' < input_file > output_file

where

\r - carriage return
\n - linefeed

Example: Delete Newlines (CRLF) with tr
 
    tr -d '\r\n' < example.txt > example2.txt
    tr example delete newline CRLF.png

Command Line Examples

Note: The command cat will concatenate files and print on the standard output. The option A will use ^ and M notation to show non-printing characters, display $ at end of each line, and display TAB characters as ^I.

REF:

http://www.canbike.org/information-technology/sed-delete-carriage-returns-and-linefeeds-crlf.html

ABHD12
    ABI2
    ACSM2A
    ACSM2B
    AGPAT5
    ANP32B
    APP
    ARID1A
    ARL10
    BACH2
    FBXL2
    FBXO22
    FBXO30
    FBXO39

最新文章

  1. BZOJ 1901: Zju2112 Dynamic Rankings[带修改的主席树]【学习笔记】
  2. MySQL 5.6.17 rpm 文件安装顺序
  3. Raspberry Pi 3 --- identify the version of linux kernal file
  4. Tomcat_启动多个tomcat时,会报StandardServer.await: Invalid command &#39;&#39; received错误
  5. leetcode 137
  6. 制作自己的Cydia发布源
  7. 李洪强iOS开发之【零基础学习iOS开发】【02-C语言】08-基本运算
  8. Component migration documentation
  9. Timus 1796. Amusement Park 聪明题
  10. 使用IntelliJ IDEA开发SpringMVC网站(一)开发环境
  11. 深入理解Java NIO
  12. Clean http handlers in Go
  13. 基于jQuery扁平多颜色选项卡切换代码
  14. 最新版本elasticsearch本地搭建入门篇
  15. redis缓存设计
  16. [luogu P3384] [模板]树链剖分
  17. ThreadPoolExcutor
  18. collate字段详细讲解
  19. Java工具库:
  20. sql根据某一个字段重复只取第一条数据

热门文章

  1. &lt;keep-alvie&gt;&lt;/keep-alive&gt;
  2. python 修改xml文档 ing
  3. 基于word2vec训练词向量(一)
  4. JavaUtil smtp 邮件发送
  5. The Little Prince-12/17
  6. 离开(切换)当前页面时改变页面title
  7. ACM总结——2017湖南省省赛总结
  8. POJ 1308 Is It A Tree?和HDU 1272 小希的迷宫
  9. OpenCV入门笔记(七) 文字区域的提取
  10. curl命令基本使用小总结