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 ...
随机推荐
- Metasploit渗透技巧:后渗透Meterpreter代理
Metasploit是一个免费的.可下载的渗透测试框架,通过它可以很容易地获取.开发并对计算机软件漏洞实施攻击测试.它本身附带数百个已知软件漏洞的专业级漏洞攻击测试工具. 当H.D. Moore在20 ...
- 多语言(Java、.NET、Node.js)混合架构下开源调用链追踪APM项目初步选型
1. 背景 我们的技术栈包括了Java..NET.Node.js等,并且采用了分布式的技术架构,系统性能管理.问题排查成本越来越高. 2. 基本诉求 针对我们的情况,这里列出了选型的主要条件,作为最终 ...
- latex与word之间的各种转化方法
https://blog.csdn.net/communix/article/details/49965233 https://blog.csdn.net/yaoqi_isee/article/det ...
- codeforces 980A Links and Pearls
题意: 有珍珠和线,问能否重新安排使得相邻珍珠之间的线的数量相等. 思路: 首先,珍珠为0或者线为0,那么都满足条件: 其次,如果珍珠的个数大于线的个数,那么肯定不满足条件: 然后,如果线的个数能够被 ...
- Python学习记录之(五)-----类进阶篇
静态方法 类方法 属性方法 通过@staticmethod装饰器即可把其装饰的方法变为一个静态方法,什么是静态方法呢?其实不难理解,普通的方法,可以在实例化后直接调用,并且在方法里可以通过self.调 ...
- android studio eclipse keymap theme 快捷键 主题风格设置
android studio eclipse keymap theme 快捷键 主题风格设置 将Android Studio的快捷键设置与eclipse一致,使用习惯的快捷键才顺手.Mac系统下:进入 ...
- POJ 1789 Truck History (Kruskal 最小生成树)
题目链接:http://poj.org/problem?id=1789 Advanced Cargo Movement, Ltd. uses trucks of different types. So ...
- Java动态菜单添加
自己做出来的添加数据库配置好的动态菜单的方法 private void createMenu() { IMenuDAO dao = new MenuDAOImpl(); String sql1 = ...
- 手头没证书,如何给https做代理?Nginx TCP转发
线上的一个海外充值接口(https)经常因我朝网络问题中断,想借助hk的机器做个https反向代理又没证书. 一开始 一开始想到的办法是借助Nginx的tcp转发进行代理: 编译NGINX时加入 -- ...
- linux交换区使用过多导致的性能问题
近日,我们开发发现有一台配置相同的服务器跑的特别慢,相同数据量的情况下,其他服务器只要跑10分钟,这台服务器要跑50分钟,经确认,所有的应用层配置参数都相同.上去之后,发现该服务器swap使用比较多, ...