sed 替换换行回车
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 替换换行回车的更多相关文章
- sed替换换行符“\n”
linux sed命令,如何替换换行符“\n” 在一次sed使用中,执行命令: sed "s/\n//g" file 1 发现,没起到任何效果. 后来,经查sed官方用户手册,才得 ...
- sqlserver查询的结果复制到excel替换掉回车换行
从sqlserver查询统计出的结果复制到excel,如果有回车,换行 ,或回车换行 ,复制到excel显示会乱会错版,查询的时候就替换掉回车换行,复制出来就不会乱了 ) ), ),),'') fro ...
- JS替换空格回车换行符
JS替换空格回车换行符 str=str.replace(/\r/g," ") str=str.replace(/\n/g,"<br />") 或 ...
- 替换换行符:回车换行CR/LF
windows采用回车+换行CR/LF表示下一行,UNIX/Linux使用换行符LF表示下一行,MAC OS系统使用用回车符CR表示下一行. CR使用符号'\r'表示, ASCII码是13: LF使用 ...
- php去除换行(回车换行)的方法
php去除换行(回车换行)的三种方法. 代码: <?php //php 不同系统的换行 //不同系统之间换行的实现是不一样的 //linux 与unix中用 \n //MAC ...
- sed 删除换行符
sed 删除换行符 sed ':label;N;s/\n/:/;b label' filename sed ':label;N;s/\n/:/;t label' filename 上面的两条命令可以实 ...
- Python3学习之路~2.8 文件操作实现简单的shell sed替换功能
程序:实现简单的shell sed替换功能 #实现简单的shell sed替换功能,保存为file_sed.py #打开命令行输入python file_sed.py 我 Alex,回车后会把文件中的 ...
- mysql去除字段内容的空格和换行回车
MySQL 去除字段中的换行和回车符 解决方法: UPDATE tablename SET field = REPLACE(REPLACE(field, CHAR(10), ''), ...
- Python3.5 day3作业一:实现简单的shell sed替换功能
需求: 1.使python具有shell中sed替换功能. #!/usr/bin/env python #_*_conding:utf-8_*_ #sys模块用于传递参数,os模块用于与系统交互. i ...
随机推荐
- 设置一个div网页滚动时,使其固定在头部,当页面滚动到距离头部300px时,隐藏该div,另一个div在底部,此时显示;当页面滚动到起始位置时,头部div出现,底部div隐藏
设置一个div网页滚动时,使其固定在头部,当页面滚动到距离头部300px时,隐藏该div,另一个div在底部,此时显示: 当页面滚动到起始位置时,头部div出现,底部div隐藏 前端代码: <! ...
- Palindrome Bo (预处理 + 区间DP)
先进行离散化,然后再预处理出所有位置的下一个元素,做好这一步对时间的优化非常重要. 剩下的就是一般的DP了.区间DP #include<bits/stdc++.h> using names ...
- 前端 dojo
http://dojotoolkit.org/documentation/tutorials/1.10/hello_dojo/ html在线编辑器 国内 http://runjs.cn 国外 http ...
- 集合List
//数组 存值长度固定,类型固定 //集合 长度不固定,类型也可以不固定 List<int> list = new List<int>(); //list.Add(78); ; ...
- 【Hadoop学习之一】Hadoop介绍
一.概念 Hadoop是一个能够对大量数据进行分布式处理的软件框架,充分利用集群的威力进行高速运算和存储. 二.主要模块Hadoop Common:支持其他Hadoop模块的常用实用程序.Hadoop ...
- 【Linux学习一】命令查看与帮助
环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 一.Linux执行命令流程:shell->bash(解释器 执行 ...
- 20165305 苏振龙《Java程序设计》第八周课上测试补做
1. 下载附件中的world.sql.zip, 参考http://www.cnblogs.com/rocedu/p/6371315.html#SECDB,导入world.sql,提交导入成功截图 2. ...
- 设计模式之State(状态)(转)
State的定义: 不同的状态,不同的行为;或者说,每个状态有着相应的行为. 何时使用? State模式在实际使用中比较多,适合"状态的切换".因为我们经常会使用If elseif ...
- flask 使用Flask-WTF处理表单
使用Flask-WTF处理表单 扩展Flask-WTF继承了WTFforms,使用它可以在flask中更方便的使用WTForms.Flask-WTF将表单数据解析.CSRF保护.文件上传等功能与Fla ...
- 远程服务调用RPC框架介绍,微服务架构介绍和RPC框架对比,dubbo、SpringClound对比
远程服务调用RPC框架介绍,微服务架构介绍和RPC框架对比,dubbo.SpringClound对比 远程服务调用RPC框架介绍,RPC简单的来说就是像调用本地服务一样调用远程服务. 分布式RPC需要 ...