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

sed 替换换行回车的更多相关文章

  1. sed替换换行符“\n”

    linux sed命令,如何替换换行符“\n” 在一次sed使用中,执行命令: sed "s/\n//g" file 1 发现,没起到任何效果. 后来,经查sed官方用户手册,才得 ...

  2. sqlserver查询的结果复制到excel替换掉回车换行

    从sqlserver查询统计出的结果复制到excel,如果有回车,换行 ,或回车换行 ,复制到excel显示会乱会错版,查询的时候就替换掉回车换行,复制出来就不会乱了 ) ), ),),'') fro ...

  3. JS替换空格回车换行符

    JS替换空格回车换行符 str=str.replace(/\r/g," ") str=str.replace(/\n/g,"<br />")  或 ...

  4. 替换换行符:回车换行CR/LF

    windows采用回车+换行CR/LF表示下一行,UNIX/Linux使用换行符LF表示下一行,MAC OS系统使用用回车符CR表示下一行. CR使用符号'\r'表示, ASCII码是13: LF使用 ...

  5. php去除换行(回车换行)的方法

    php去除换行(回车换行)的三种方法. 代码: <?php     //php 不同系统的换行   //不同系统之间换行的实现是不一样的   //linux 与unix中用 \n   //MAC ...

  6. sed 删除换行符

    sed 删除换行符 sed ':label;N;s/\n/:/;b label' filename sed ':label;N;s/\n/:/;t label' filename 上面的两条命令可以实 ...

  7. Python3学习之路~2.8 文件操作实现简单的shell sed替换功能

    程序:实现简单的shell sed替换功能 #实现简单的shell sed替换功能,保存为file_sed.py #打开命令行输入python file_sed.py 我 Alex,回车后会把文件中的 ...

  8. mysql去除字段内容的空格和换行回车

    MySQL 去除字段中的换行和回车符 解决方法:          UPDATE tablename SET field = REPLACE(REPLACE(field, CHAR(10), ''), ...

  9. Python3.5 day3作业一:实现简单的shell sed替换功能

    需求: 1.使python具有shell中sed替换功能. #!/usr/bin/env python #_*_conding:utf-8_*_ #sys模块用于传递参数,os模块用于与系统交互. i ...

随机推荐

  1. “编程利器”:VSCode

    原先一直使用sublime text3,并且认为它是很好的编程利器. 但最近写代码时,发现很多代码还是提示的不够完整.我们知道,当代码名字很长时,还没有提醒,这是非常苦恼的一件事!同时它的调试功能也不 ...

  2. 基础 MySQL

    .一.MySQL概述 1.什么是数据库  答:数据的仓库,如:在ATM的示例中我们创建了一个 DB 目录,称其为数据库 2.什么是 MySQL.Oracle.SQLite.Access.MS SQL ...

  3. Spring boot FastJson

    介绍:FastJson 是ailibaba 的一款解析Json的开源框架 使用方式1 引入jar 包 <dependency>    <groupId>com.alibaba& ...

  4. TCP协议的三次握手

    TCP协议是面向连接的通信协议,即在传输数据前先在发送端和接收端建立逻辑连接,然后再传输数据,它提供了两台计算机之间可靠无差错的数据传输. l  IP地址:用来唯一表示我们自己的电脑的,是一个网络标示 ...

  5. EF使用sql语句

    https://www.cnblogs.com/chenwolong/p/SqlQuery.html https://blog.csdn.net/zdhlwt2008/article/details/ ...

  6. 初学delphi

    今天女朋友的一门课,要求用delphi 软件编程,内容是一个计算器.当然,这个工作肯定是落在我的头上了. 这个软件是我第一次使用,边自学边进行代码编写,在n多次修改完善之后,终于成形.功能不是很多,跟 ...

  7. mongoDB 的介绍

    一.常用的网站 MongoDB  --  2009年被发布 MongoDB的官网:  www.mongodb.org 可以下载安装包    和  使用文档 MongoDB国内官方网站:  www.mo ...

  8. org.springframework.jdbc.UncategorizedSQLException

    org.springframework.jdbc.UncategorizedSQLException: StatementCallback; uncategorized SQLException fo ...

  9. gcahce事物不够,借助binlog追上

    gcahce事物不够,借助binlog追上 宕机节点以单机集群启动,既自己作为一个集群启动,不过UUID要和旧的集群保持一致: 修复grastate.dat 文件的方式这里略,直接通过wsrep_re ...

  10. 最新版Postman的下载及安装

    1. 操作环境 Windows Windows 7旗舰版 64位  , Windows 10专业版 Postman Postman-win64-5.1.3-Setup.exe 2. Postman的资 ...