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. 强化学习--Policy Gradient

    Policy Gradient综述: Policy Gradient,通过学习当前环境,直接给出要输出的动作的概率值.   Policy Gradient  不是单步更新,只能等玩完一个epoch,再 ...

  2. 如何注销Sitecore CMS

    登录Sitecore很容易,但是在旧版本的Sitecore中使用不同的界面,退出可能会给未经证实的人带来挑战. Sitecore 8 Sitecore 6和7 Sitecore 8 Sitecore ...

  3. idea 项目转 eclipse项目

    接到一个很紧急的活,我很着急,也很兴奋,打开邮件一看,有点懵逼.   idea项目.idea不熟啊,网上搜攻略.我做个总结,归根结底就是一句话.   下个idea,然后一步一步的安装好.   然后也是 ...

  4. Shell 比较两个数的大小

    格式很重要多一个空格少一个空格都可能出错 li@ubuntu:~/test$ cat compare.sh #!/bin/bash read x read y if [ $x -lt $y ] the ...

  5. Top 5 Reasons to Get BMW ICOM A2 with Latest Software

    Top 5 Reasons to Get BMW ICOM A2 with Latest Software 1.BMW ICOM A2 Hardware Version: V2018.03 2.Sup ...

  6. 爬虫--cheerio

    const cheerio = require('cheerio') const $ = cheerio.load('<h2 class="title">Hello w ...

  7. Eclipse导入MyEclipse创建的WEB项目无法识别的解决方案

    Eclipse导入MyEclipse创建的WEB项目无法识别的解决方案

  8. excel 方框打钩

    将光标定位于需要打钩的地方,选择[插入]→[符号]→[其他符号] 在弹出的符号栏里,字体一定要改成[Windings2] 然后在符号栏便可以找到现成的打钩样式,点击插入,再关闭即可 提示:如果需要打叉 ...

  9. The logback manual #03# Configuration

    索引 Configuration in logback Automatically configuring logback Automatic configuration with logback-t ...

  10. kswapd0 进程CPU占用过高

    前几天遇到的一个问题,自己本地用VM配置的虚拟机,一般会top查看进程以及CPU占用的一些情况.又一次用laravel 打印对象,里面的内容比较多,浏览器当时就卡了. 然后看进程的情况.我以为会是ng ...