linux sed文本
- 常规编辑器编辑困难的文本。
- 太过庞大的文本,使用常规编辑器难以胜任(如vi一个几百兆的文件)。
- 有规律的文本修改,加快文本处理速度(比如说全文替换)。
sed命令 | 作用 |
a | 在匹配行后面加入文本 |
c | 字符转换 |
d | 删除行 |
D | 删除第一行 |
i | 在匹配行前面接入文本 |
h |
复制模板块的内容到存储空间 |
H | 追加模板块的内容到存储空间 |
g | 将存储空间的内容复制到模式空间 |
G | 将存储空间的内容追加到模式空间 |
n | 读取下一个输入行,用下一个命令处理新的行 |
N | 追加下一个输入行到模板块后并在二者间插入新行 |
p | 打印匹配的行 |
P | 打印匹配的第一行 |
q | 退出sed |
r | 从外部文件中读取文本 |
w | 追加写文件 |
! | 匹配的逆 |
s/old/new | 用new替换正则表达式old |
= | 打印当前行号 |
sed常用的参数:
sed参数 | 作用 |
-e | 多条件编辑 |
-h | 帮助信息 |
-n | 不输出不匹配的行 |
-f | 指定sed脚本 |
-V | 版本信息 |
-i | 直接修改原文件 |
sed常用的正则表达式匹配:
元字符 | 作用 |
^ | 匹配行的开始。如:/^cat/匹配所有以cat开头的行 |
$ | 匹配行的结束。如:/cat$/匹配所有以cat结尾的行 |
. | 匹配任一非换行字符。如:/c.t/匹配c后接一个任意字符,然后是t |
* | 匹配零个或任意多个字符。如:/*cat/匹配一串字符后紧跟cat的所有行 |
[] | 匹配指定范围内的字符。如:/[Cc]at/匹配cat和Cat |
[^] | 匹配指定范围外的任意单个字符。如:/[^A-Z]/匹配没有大写字母的行 |
\(..\) | 保存匹配的字符。如:s/\(love\)able/\1rs/, loveable被替换成lovers |
& | 保存搜索字符用来替换其他字符。如:s/love/**&**/,love编程**love** |
\< | 锚定单词的开始。如:/\<cat/匹配包含以cat开头的单词的行 |
\> | 锚定单词的结尾。如:/cat\>/匹配包含以cat结尾的单词的行 |
[x\{n\} | 重复字符x,m次。如:/o\{5\}/匹配包含5个o的行 |
x\{m,\} | 重复字符x,至少m次。如:/o\{5,\}/匹配至少有5个o的行 |
x\{n,m\} | 重复字符x,至少m次,不多于n次。如:/o\{5,10\}/匹配5到10个o的行 |
[root@kurol ~]# cat sed.txt
this is line 1, this is First line
this is line 2, the Second line, Empty line followed
this is line 4, this is Third line
this is line 5, this is Fifth line
sed [option] 'command' file
#option 是sed可以接受的参数
#command 是sed的命令集(一共有25个)
#使用-e参数和分号连接多编辑命令
#该参数本身只是sed的一个简单参数,表示将下一个字符串解析为sed编辑命令
#一般情况下可以忽略,但是当sed需要传递多个编辑命令时该参数就不能少了
下面的例子演示了将this改为that的同时,还要讲line改为LINE,两个编辑命令前都要使用-e参数,如果有更多的编辑需求,以此类推
[root@kurol ~]# sed -e 's/this/that/g' -e 's/line/LINE/g' sed.txt
that is LINE 1, that is First LINE
that is LINE 2, the Second LINE, Empty LINE followed
that is LINE 4, that is Third LINE
that is LINE 5, that is Fifth LINE
使用分号(;)连接两个都编辑的命令,上面的命令用分号也可达到同样的效果:
[root@kurol ~]# sed 's/this/that/g ; s/line/LINE/g' sed.txt
that is LINE 1, that is First LINE
that is LINE 2, the Second LINE, Empty LINE followed
that is LINE 4, that is Third LINE
that is LINE 5, that is Fifth LINE
删除
使用d命令可删除指定的行:
#将file的第一行删除后输出到屏幕
[root@kurol ~]# sed '1d' sed.txt
this is line 2, the Second line, Empty line followed
this is line 4, this is Third line
this is line 5, this is Fifth line
由于sed默认不修改原文件,如果希望保存修改后的文件则需要用重定向:
sed '1d' sed.txt > saved_file
如果想直接修改文件,使用 -i 参数,这样的方式不会有任何输出,而是直接修改了源文件
sed -i '1d' sed.txt
删除指定范围的行 :
删除1-3行:
[root@kurol ~]# sed '1,3d' sed.txt
this is line 4, this is Third line
this is line 5, this is Fifth line
删除第一行当最后一行:
[root@kurol ~]# sed '1,$d' sed.txt
[root@kurol ~]# #清空了sed.txt文件
删除最后一行:
[root@kurol ~]# sed '$d' sed.txt
this is line 1, this is First line
this is line 2, the Second line, Empty line followed
this is line 4, this is Third line
删除指定范围以外的行(只保留第五行):
[root@kurol ~]# sed '5!d' sed.txt
this is line 5, this is Fifth line
删除所有包含Empty的行:
[root@kurol ~]# sed '/Empty/d' sed.txt
this is line 1, this is First line
this is line 4, this is Third line
this is line 5, this is Fifth line
删除空行:
[root@kurol ~]# sed '/^$/d' sed.txt
this is line 1, this is First line
this is line 2, the Second line, Empty line followed
this is line 4, this is Third line
this is line 5, this is Fifth line
查找替换
使用s命令可将查找到的匹配文本内容替换成新的文本
s命令用于替换文本
将每行第一个line替换成LINE:
[root@kurol ~]# sed 's/line/LINE/' sed.txt
this is LINE 1, this is First line
this is LINE 2, the Second line, Empty line followed
this is LINE 4, this is Third line
this is LINE 5, this is Fifth line
以上只是把每一行的第一个line被替换了,'s/old/new/' 默认情况下只替换第一次匹配到的内容.
将每行匹配到2个line,并改为LINE:
[root@kurol ~]# sed 's/line/LINE/2' sed.txt
this is line 1, this is First LINE
this is line 2, the Second LINE, Empty line followed
this is line 4, this is Third LINE
this is line 5, this is Fifth LINE
s命令利用g选项,可以完成所有匹配值的替换(全文替换):
[root@kurol ~]# sed 's/line/LINE/g' sed.txt
this is LINE 1, this is First LINE
this is LINE 2, the Second LINE, Empty LINE followed
this is LINE 4, this is Third LINE
this is LINE 5, this is Fifth LINE
字符转换
使用y命令可进行字符转换,其作用为将一系列字符逐个地变换为另外一系列字符,基本用法如下:
sed 'y/old/new/' file
该命令会将file中的o转换为n、l转换成e、d转换成w
注意转换字符和被转换字符的长度要相等,否则sed无法执行
将数字1转换为A,2转换为B,4转换为D,5转换为E:
[root@kurol ~]# sed 'y/1245/ABDE/' sed.txt
this is line A, this is First line
this is line B, the Second line, Empty line followed
this is line D, this is Third line
this is line E, this is Fifth line
插入文本
使用i或a命令插入文本,其中i代表在匹配行之前插入,而a代表在匹配行之后插入
使用i在第二行前插入文本:
[root@kurol ~]# sed '2 i Insert' sed.txt
this is line 1, this is First line
Insert
this is line 2, the Second line, Empty line followed
this is line 4, this is Third line
this is line 5, this is Fifth line
使用a在第二行后插入文本:
[root@kurol ~]# sed '2 a Insert' sed.txt
this is line 1, this is First line
this is line 2, the Second line, Empty line followed
Insert
this is line 4, this is Third line
this is line 5, this is Fifth line
在匹配行的上一行插入文本:
[root@kurol ~]# sed '/Second/i\Insert' sed.txt
this is line 1, this is First line
Insert
this is line 2, the Second line, Empty line followed
this is line 4, this is Third line
this is line 5, this is Fifth line
读入文本
使用r命令可从其他文件中读取文本,并插入匹配行之后
将/etc/passwd 中的内容读出放到sed.txt空号之后
[root@kurol ~]# sed '/^$/r /etc/passwd' sed.txt
this is line 1, this is First line
this is line 2, the Second line, Empty line followed
root:x:0:0:root:/root:/bin/bash
......
nginx:x:498:499:Nginx web server:/var/lib/nginx:/sbin/nologin
this is line 4, this is Third line
this is line 5, this is Fifth line
打印
使用p命令可进行打印,这里使用sed命令时一定要加-n参数,表示不打印没关系的行。从之前的例子可以看出,由于sed的工作原理是基于行的,因此每次都有大量的输出。可是这些输出中有一些我们并不需要看到的,而只需要输出匹配的行或者处理过的行就好了。简单来说,打印操作是删除操作的“逆操作”。
打印出文件中指定的行:
[root@kurol ~]# sed -n '1p' sed.txt
this is line 1, this is First line
将the替换成THE,sed实际处理了第二行,其他几行由于没有匹配所以并未真正处理,但是sed的工作原理是基于流的,所以所有流过的行都打印出来了:
[root@kurol ~]# sed 's/the/THE/' sed.txt
this is line 1, this is First line
this is line 2, THE Second line, Empty line followed
this is line 4, this is Third line
this is line 5, this is Fifth line
使用p命令,则只打印实际处理过的行,简化了输出(使用-n参数):
[root@kurol ~]# sed -n 's/the/THE/p' sed.txt
this is line 2, THE Second line, Empty line followed
写文件
sed本身默认并不改写原文件,而只是对缓存区的文本做了修改并输出到屏幕。所以想保存文件,除了之前提到的两种方法外(使用重定向或-i参数),还可以使用w命令将结果保存到外部指定文件。
[root@kurol ~]# sed -n '1,2 w output' sed.txt
[root@kurol ~]# #这里没有任何输出,因为输出被重定向到output文件了
[root@kurol ~]# cat output
this is line 1, this is First line
this is line 2, the Second line, Empty line followed
sed脚本
在平日的工作中,需要定期对一些文件做分析操作,这种例行的工作往往有一定“标准化” 的操作,比如说先去除文件中所有的空行,然后再全部替换某些字符等,这种过程类似于生产线上程式化的流水作业。事实上,可以把这些动作静态化地写到某个文件中,然后调用sed命令并使用-f参数指定该文件,这样就可以将一系列动作“装载”并应用于指定文件中,这无疑加快了工作效率,这种文件就是sed脚本。
如,创建sed.rules脚本文件,该sed脚本的作用是将全文的this改为THAT,并删除所有空号
[root@kurol ~]# cat sed.rules
s/this/THAT/g
/^$/d
[root@kurol ~]# sed -f sed.rules sed.txt #使用-f参数指定该脚本应用于sed.txt
THAT is line 1, THAT is First line
THAT is line 2, the Second line, Empty line followed
THAT is line 4, THAT is Third line
THAT is line 5, THAT is Fifth line
linux sed文本的更多相关文章
- Linux - sed 文本操作
SED 是一项Linux指令,功能同awk类似,差别在于,sed简单,对列处理的功能要差一些,awk的功能复杂,对列处理的功能比较强大. sed全称是:Stream EDitor 调用sed命令有两种 ...
- Linux的文本处理工具浅谈-awk sed grep
Linux的文本处理工具浅谈 awk 老大 [功能说明] 用于文本处理的语言(取行,过滤),支持正则 NR代表行数,$n取某一列,$NF最后一列 NR==20,NR==30 从20行到30行 FS ...
- awk、grep、sed是linux操作文本的三大利器,也是必须掌握的linux命令之一
awk.grep.sed是linux操作文本的三大利器,也是必须掌握的linux命令之一.三者的功能都是处理文本,但侧重点各不相同,其中属awk功能最强大,但也最复杂.grep更适合单纯的查找或匹配文 ...
- Linux Bash文本操作之sed篇其二
上一篇总结了sed的基础应用(Linux Bash文本操作之sed篇其一),内容实在有够多,这里再对稍微高级一些的用法做一个整理,以方便使用时查阅. 查看文本内容 示例1表示在第一到第四行匹配到的行后 ...
- Linux sed 替换第一次出现的字符串
/********************************************************************************* * Linux sed 替换第一次 ...
- linux sed命令参数及用法详解
linux sed命令参数及用法详解 http://blog.csdn.net/namecyf/article/details/7336308 1. Sed简介 sed 是一种在线编辑器,它一次处理一 ...
- [转帖]linux sed命令
linux sed命令就是这么简单 https://www.cnblogs.com/wangqiguo/p/6718512.html 用到的最多的就是一个sed -i 's/nn/mm/' 的命令了. ...
- Linux sed命令使用方法
sed(Stream Editor)是Linux中文本处理使用非常广泛的工具,可以对文件内容进行替换.删除.新增.选取特定行等功能.下面通过sed常用实例介绍sed命令的使用方法. sed基本语法 s ...
- 理解linux sed命令
理解linux sed命令(2010-02-27 18:21:20) 标签:linuxshellsed替换 分类:革命本钱 1. Sed简介sed是一种在线编辑器,它一次处理一行内容.处理时,把当 前 ...
随机推荐
- python 回溯法 子集树模板 系列 —— 14、最长公共子序列(LCS)
问题 输入 第1行:字符串A 第2行:字符串B (A,B的长度 <= 1000) 输出 输出最长的子序列,如果有多个,随意输出1个. 输入示例 belong cnblogs 输出示例 blog ...
- linux 定时器原理
内核定时器: unsigned long timeout = jiffies + (x * HZ); while(1) { // Check the condition. ...
- [BZOJ4851][JSOI2016]位运算[矩阵快速幂]
题意 给定长度为 \(\rm |S|\) 的 \(\rm 01\) 串并将其倍长 \(k\) 次得到一个 \(\rm|S|\times k\) 位的二进制数 \(R\) ,求有多少种在 \([0,R- ...
- Hive的一些理解
首先谈一下关于hive和hbase的区别的疑问(完全不是一个东西): 本质上来说hive和hbase没什么关系,虽然都是表,查数据等,但是他们根本就不是一个层面的东西 hive就是一个rapduce的 ...
- 冒泡排序算法的C++,Java和Python实现和冒泡排序算法三种语言效率的比较
冒泡排序原理: 这一篇百度经验讲得很好,我不多说了 https://jingyan.baidu.com/article/6525d4b13f920bac7d2e9484.html 他讲的是C语言,没有 ...
- 自动化部署-Jenkins+SVN+MSBuild 一些补充
1.ftp的使用 系统管理->插件管理 安装插件Publish Over FTP 系统管理->系统设置 配置ftp参数 如下图 进入任务配置,添加构建后操作 在使用过程中还遇到一个本地防火 ...
- QUIC和TCP
作者:henrystark henrystark@126.com Blog: http://henrystark.blog.chinaunix.net/ 日期:20140626 本文遵循CC协议:署名 ...
- ajax请求超时判断(转载)
ajax请求时有个参数可以借鉴一下 var ajaxTimeOut = $.ajax({ url:'', //请求的URL timeout : 1000, //超时时间设置,单位毫秒 type : ' ...
- Alpha版总结会议——班级派
一.开会的过程 在周一下午上课的最后20分钟内,我们组进行了“班级派”的alpha版的总结会议.首先进行的是分析目前的版本情况,每个人说了自己的进度,包括已经完成的以及即将要完成的.随后是分析前段时间 ...
- navicat连接mysql报10061错
可能原因:mysql服务未启动 解决办法:进入到计算机管理,找到服务,然后找到mysql服务,并启动该服务