sed 流编辑命令
1.命令功能
sed非交互式的流编辑器,sed不会修改源文件内容,除非重定向来保存输出结果;默认情况下所有的输出行都将被打印到屏幕上。
2.语法格式
sed [option] {script-only-if-no-other-script} [input-file]
sed 选项 ‘操作命令’ 输入文本
sed 常用选项说明
|
选项 |
功能 |
|
-n |
安静模式,只输出被sed处理的行 |
|
-f |
指定一个sed脚本文件到命令行执行 |
|
-r |
sed使用扩展正则 |
|
-i |
直接修改文件读取的内容,不在屏幕上输出 |
sed操作命令
|
sed命令 |
功能 |
|
a\ |
在当前行后添加一行或多行文本 |
|
i\ |
在当前行插入文本 |
|
q |
结束或退出sed |
|
r |
从文件中读取输入行 |
|
c\ |
用文本替换或修改选中的行 |
|
d |
删除行 |
|
h[H] |
复制[追加]模式空间中的内容到缓存区 |
|
g |
将缓存区的内容,复制到模式空间,覆盖该处原有的内容 |
|
G |
将缓存区的内容,复制到模式空间,追加在原有内容后面 |
|
p |
打印行 |
|
替换标志 |
|
|
s/regexp/replacement/ |
将regexp内容替换成replacement |
|
g |
在行内全局替换 |
|
p |
打印行 |
|
w |
将行写入文件 |
|
x |
交换暂存缓冲区与模式空间的内容 |
|
y | y/source/dest/ |
将字符转换成另一个字符(不能对正则表达式使用y命令) |
3.使用范例
示例1 p命令,打印包含root的行
[root@localhost ~]# sed '/root/p' test.txt
root:x:0:0:root:/root:/bin/bash
root:x:0:0:root:/root:/bin/bash #打印内容
bin:x:1:1:bin:/bin:/sbin/nologin
示例 2 n安静模式,只打印包含root的行
[root@localhost ~]# sed -n '/root/p' test.txt
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
示例3 d删除命令,删除行
[root@localhost ~]# cat -n test.txt
1 root:x:0:0:root:/root:/bin/bash
2 bin:x:1:1:bin:/bin:/sbin/nologin
3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
4 adm:x:3:4:adm:/var/adm:/sbin/nologin
5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
6 sync:x:5:0:sync:/sbin:/bin/sync
7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
8 halt:x:7:0:halt:/sbin:/sbin/halt
[root@localhost ~]# sed '3d' test.txt #第三行“daemon:x:2:2:daemon:/sbin:/sbin/nologin”被删除
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
删除第1行到第3行
[root@localhost ~]# sed '1,3d' test.txt
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
删除包含root的行
[root@localhost ~]# sed '/root/d' test.txt #删除包含root的行
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
示例4 替换:s命令
全局nologin替换成no
[root@localhost ~]# sed 's/nologin/no/g' test.txt #s表示替换,g表示全局
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/no
daemon:x:2:2:daemon:/sbin:/sbin/no
adm:x:3:4:adm:/var/adm:/sbin/no
lp:x:4:7:lp:/var/spool/lpd:/sbin/no
注:如果没有g,只会替换每一行的第一个nologin。
示例5 a追加命令
在第2行后追加“hello boy welcome to linux”
[root@localhost ~]# sed '2a hello boy welcome to linux' test.txt
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
hello boy welcome to linux #在第2行后追加内容
daemon:x:2:2:daemon:/sbin:/sbin/nologin
在第2行后追加2行“hello world\nwelcome to linux”
[root@localhost ~]# sed '2a hello world\nwelcome to linux' test.txt
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
hello world #追加的第一行
welcome to linux #追加的第二行
daemon:x:2:2:daemon:/sbin:/sbin/nologin
示例6 i插入命令
在第2行位置插入”hello world”
[root@localhost ~]# sed '2i hello world' test.txt
root:x:0:0:root:/root:/bin/bash
hello world #在第2行插入的内容
bin:x:1:1:bin:/bin:/sbin/nologin
在第2行位置插入2行“hello world\nwelcome to linux”
[root@localhost ~]# sed '2i hello world\nwelcome to linux' test.txt
root:x:0:0:root:/root:/bin/bash
hello world #插入的第一行
welcome to linux #插入的第二行
bin:x:1:1:bin:/bin:/sbin/nologin
示例7 d删除命令
删除第2行
[root@localhost ~]# sed '2d' test.txt #源文件第二行的内容’ bin:x:1:1:bin:/bin:/sbin/nologin’
root:x:0:0:root:/root:/bin/bash
daemon:x:2:2:daemon:/sbin:/sbin/nologin
[root@localhost ~]# cat -n test.txt |sed '2d'
1 root:x:0:0:root:/root:/bin/bash
3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
删除第2行到第4行
[root@localhost ~]# cat -n test.txt |sed '2,4d'
1 root:x:0:0:root:/root:/bin/bash
5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
示例8 p输出指定行
输出文件第2行内容
[root@localhost ~]# cat -n test.txt |sed '2p'
1 root:x:0:0:root:/root:/bin/bash
2 bin:x:1:1:bin:/bin:/sbin/nologin
2 bin:x:1:1:bin:/bin:/sbin/nologin #p输出的内容
示例9 sed 选项-i命令使用
-i 直接写入到文件里,不在屏幕上现象
在test.txt文件25行插入写入如下几行
hello boy
welcome to linux
linux is fun
thank you
[root@localhost ~]# sed -i '25i hello boy\nwelcome to linux\nlinux is fun\nthank you' test.txt
[root@localhost ~]# cat -n test.txt |tail -7
23 joe:x:502:502::/home/joe:/bin/bash
24 chu:x:503:503::/home/chu:/bin/bash
25 hello boy #插入内容
26 welcome to linux #插入内容
27 linux is fun #插入内容
28 thank you #插入内容
29 dbus:x:81:81:System message bus:/:/sbin/nologin
示例10 打印指定范围内的行
[root@localhost ~]# sed -n '/joe/,/chu/p' test.txt
joe:x:502:502::/home/joe:/bin/bash
chu:x:503:503::/home/chu:/bin/bash
示例11 修改:c命令
c命令将修改模式空间中的内容,模式空间中的内容被修改,源文件内容不变。
[root@localhost ~]# sed '/root/c hello boy\' test.txt
hello boy
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
[root@localhost ~]# cat test.txt #源文件内容
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
示例12 转换:y 命令
把root字符串对应转换成AAAA
[root@localhost ~]# sed 'y/root/AAAA/' test.txt
AAAA:x:0:0:AAAA:/AAAA:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nAlAgin
daemAn:x:2:2:daemAn:/sbin:/sbin/nAlAgin
示例13 取出网卡IP地址
[root@localhost ~]# ifconfig |sed -n '/inet addr/p' |cut -d ":" -f2 |cut -d ' ' -f1
172.16.2.10
127.0.0.1
sed 流编辑命令的更多相关文章
- 文本处理三剑客之 Sed ——高级编辑命令
本篇介绍sed的高级编辑命令 高级编辑命令 P:打印模式空间开端至\n内容,并追加到默认输出之前 n: 读取匹配到的行的下一行覆盖至模式空间 N:读取匹配到的行的下一行追加至模式空间 h: 把模式空间 ...
- 文本处理三剑客之 Sed ——一般编辑命令
sed简介 sed (stream editor for filtering and transforming text) 是Linux上的文本处理三剑客之一,另外两个是grep和awk. sed又称 ...
- Linux下的sed流编辑器命令详解
sed是stream editor的简称,也就是流编辑器.它一次处理一行内容,处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内 ...
- mac上执行sed的编辑 -i命令报错sed: 1: "test.txt": undefined label ‘est.txt’或sed: 1: "2a\test\": extra characters after \ at the end of a command
问题一 sed编辑命令:[sed -i 's/a/b/g' test.txt] 报错:sed: 1: "test.txt": undefined label 'est.txt' ...
- grep,awk和sed的常用命令和语法
Grep的常用命令语法 1. 双引号引用和单引号引用在g r e p命令中输入字符串参数时,最好将其用双引号括起来.例如:“m y s t r i n g”.这样做有两个原因,一是以防被误解为 s h ...
- Shell:sed流编辑器
转载:http://blog.sina.com.cn/s/blog_ac9fdc0b0101lvdv.html sed和awk是永远地痛,学了又忘,主要是木有横向对比过,所以总把握不到精髓.它可以完美 ...
- shell基础 -- grep、sed、awk命令简介
在 shell 编程中,常需要处理文本,这里介绍几个文本处理命令. 一.grep 命令 grep 命令由来已久,用 grep 命令来查找 文本十分方便.在 POSIX 系统上,grep 可以在两种正则 ...
- Linux echo, sort, sed 等一些命令总结
linux echo, sort, sed是初学linux shell script 的一些常用的命令.基本上来说,如果能够掌握了这些命令,我们就能写出一些不错的linux脚本.以下是我遇到的以下常用 ...
- 正则表达式 grep文本查询 sed流处理 应用
一.正则表达: ^:以什么什么开头,^a:以a字符开头 $:以什么什么结尾,b$:以b字符结尾 *:左边字符0-无穷个 +:左边字符1-无穷个 .:代表单字符 ?:前导字符为零个或1个 {n}:左面字 ...
随机推荐
- Centos7源码安装jdk
1. 下载jdk : jdk-8u211-linux-x64.tar.gz https://www.oracle.com/technetwork/java/javase/downloads/jdk8 ...
- instanceOf与isInstance()方法之间的区别
instanceof运算符 只被用于对象引用变量,检查左边的被测试对象 是不是 右边类或接口的 实例化.如果被测对象是null值,则测试结果总是false.Class类的isInstance(Obje ...
- 第四周总结 & 实验报告(二)
第四周课程总结 一.String类 1.实例化 (1)直接赋值 public class Xxxx{ public static void main(String args[]){ String a ...
- 十五、RF操作时间控件
由于日期控件经常用的是readonly属性,这个属性意思是此控件为可读,明白点就是只让你看,不让你动. 解决方法就是:用js去掉这个属性,就可写了,就能输入了 导入库:DateTime #方式一 op ...
- maven 安装jar包命令
以 spring-context-support-3.1.0.RELEASE.jar 为例,在 @3图中已经给出这个 jar 包的 groupId,artifactId,version信息,手动安装的 ...
- python - 多进程 Value、Array应用记录
在代码优化的过程中,碰到了这样一个问题:一个进程中我定义了几个全局变量,然后我又Process了几个子进程,子进程中是否可以各自对全局变量进行修改?最后全局变量会取哪个值呢? 经过一番尝试以后得到结果 ...
- Selenium学习之==>18种定位方式的使用
Selenium的定位方式一共有18种,单数8种,复数8种,最后两种是前面这16种的底层封装 单数形式8种 # 1.id定位 al = driver.find_element_by_id('i1') ...
- C#采集:图灵机器人信息
Dictionary<string, string> users = new Dictionary<string, string>(); users.Add("use ...
- text_to_be_present_in_element
text_to_be_present_in_element(locator,text)是指定页面元素的文本位置, 一般用于验证一个文本信息或者错误的信息,我们任然以百度登录为案例, 用户名和密码为空, ...
- 【ABAP系列】SAP 面试 ABAPer的一些感想
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP 面试 ABAPer的一些 ...