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 流编辑命令的更多相关文章

  1. 文本处理三剑客之 Sed ——高级编辑命令

    本篇介绍sed的高级编辑命令 高级编辑命令 P:打印模式空间开端至\n内容,并追加到默认输出之前 n: 读取匹配到的行的下一行覆盖至模式空间 N:读取匹配到的行的下一行追加至模式空间 h: 把模式空间 ...

  2. 文本处理三剑客之 Sed ——一般编辑命令

    sed简介 sed (stream editor for filtering and transforming text) 是Linux上的文本处理三剑客之一,另外两个是grep和awk. sed又称 ...

  3. Linux下的sed流编辑器命令详解

    sed是stream editor的简称,也就是流编辑器.它一次处理一行内容,处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内 ...

  4. 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' ...

  5. grep,awk和sed的常用命令和语法

    Grep的常用命令语法 1. 双引号引用和单引号引用在g r e p命令中输入字符串参数时,最好将其用双引号括起来.例如:“m y s t r i n g”.这样做有两个原因,一是以防被误解为 s h ...

  6. Shell:sed流编辑器

    转载:http://blog.sina.com.cn/s/blog_ac9fdc0b0101lvdv.html sed和awk是永远地痛,学了又忘,主要是木有横向对比过,所以总把握不到精髓.它可以完美 ...

  7. shell基础 -- grep、sed、awk命令简介

    在 shell 编程中,常需要处理文本,这里介绍几个文本处理命令. 一.grep 命令 grep 命令由来已久,用 grep 命令来查找 文本十分方便.在 POSIX 系统上,grep 可以在两种正则 ...

  8. Linux echo, sort, sed 等一些命令总结

    linux echo, sort, sed是初学linux shell script 的一些常用的命令.基本上来说,如果能够掌握了这些命令,我们就能写出一些不错的linux脚本.以下是我遇到的以下常用 ...

  9. 正则表达式 grep文本查询 sed流处理 应用

    一.正则表达: ^:以什么什么开头,^a:以a字符开头 $:以什么什么结尾,b$:以b字符结尾 *:左边字符0-无穷个 +:左边字符1-无穷个 .:代表单字符 ?:前导字符为零个或1个 {n}:左面字 ...

随机推荐

  1. 基本vim快捷键

    w,e等单词间跳转,英文字符(除了_),数字,符号,空格 都被vim独立看成独立的词

  2. spotlight监控linux性能

    linux性能监控有很多工具,spotlight只是其中一种 目录 1.安装spotlight 2.参数认识 1.安装spotlight spotlight不仅仅只是监控linux,还可以完成数据库以 ...

  3. 5.2.k8s.Secret

    #Secret Secret存储密码.token.密钥等敏感数据 Secret以Volume或环境变量方式使用 #Secret类型 Opaque : base64 编码格式的 Secret kuber ...

  4. 【web前端】面题整理(2)

    今天是520,单身狗在这里祝各位520快乐! DOM节点统计 DOM 的体积过大会影响页面性能,假如你想在用户关闭页面时统计(计算并反馈给服务器)当前页面中元素节点的数量总和.元素节点的最大嵌套深度以 ...

  5. numpy的linalg.norm()函数求范数

    函数签名:def norm(x, ord=None, axis=None, keepdims=False) 其中ord参数表示求什么类型的范数,具体参见下表 下面是用代码对一个列表求上面的范数 imp ...

  6. 操作系统 - Linux操作系统 - Centos - Centos7 - 安装|命令|使用汇总

    镜像: http://mirrors.aliyun.com/centos/7/isos/x86_64/http://archive.kernel.org 网络配置 - DHCP  # /etc/res ...

  7. DG on Windows 10 S: 执行任意代码

    DG on Windows 10 S: 执行任意代码 windows 10 S版本是什么鬼? 众所周知,我们使用的是windows 10企业版 LTSC.更准确一点,CMD运行winver,我的版本是 ...

  8. ssh远程登录过程中卡住

    1.首先排查网络连通性,查看网络是否通畅,远程端口是否开放 2.查看服务器复制,cpu,内存负载是否过大 3.检查ssh配置,查看以下配置是否这样配置 UseDNS no GSSAPIAuthenti ...

  9. Spring中单例模式中的饿汉和懒汉以及Spring中的多例模式

    链接:https://pan.baidu.com/s/1wgxnXnGbPdK1YaZvhO7PDQ 提取码:opjr 单例模式:每个bean定义只生成一个对象实例,每次getBean请求获得的都是此 ...

  10. C++ vector、list和deque的区别 (整理)

    1.vector数据结构 vector和数组类似,拥有一段连续的内存空间,并且起始地址不变.因此能高效的进行随机存取,时间复杂度为o(1);但因为内存空间是连续的,所以在进行插入和删除操作时,会造成内 ...