sed介绍

sed是Stream Editor(流编辑器)的缩写,简称流编辑器;用来处理文件的
sed是一行一行读取文件内容并按照要求进行处理,把处理后的结果输出到屏幕。
首先sed读取文件中的一行内容,把其保存在一个临时缓存区中(也称为模式空间)也就意味着读取巨大文件会导致临时区爆满
然后根据需求处理临时缓冲区中的行,完成后把该行发送到屏幕上,由于sed把每一行都存在临时缓冲区中,对这个副本进行编辑,所以不会直接修改原文件。sed主要用来自动编辑一个或多个文件;简化对文件的反复操作,对文件进行过滤和转换操作

sed使用方法介绍

sed常见的语法格式有两种,一种叫命令行模式,另一种叫脚本模式

1. 命令行格式

㈠ 语法格式
sed [options] ‘处理动作’ 文件名

注:写i时不要写n
常见处理动作

㈡ 举例说明
文件准备

#  vim a.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
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
298374837483
172.16.0.254
10.1.1.1

① 对文件进行增、删、改、查操作
语法:sed 选项 ‘定位+命令’ 需要处理的文件
1)打印文件内容
[root@tom ~]# sed -n ‘p’ a.txt 打印每一行
[root@tom ~]# sed -n ‘1p’ a.txt 打印第1行
[root@tom ~]# sed -n ‘2p’ a.txt 打印第2行
[root@tom ~]# sed -n ‘1,5p’ a.txt 打印1到5行
[root@tom ~]# sed -n ‘$p’ a.txt 打印最后1行

2)增加文件内容
i 地址定位的上面插入
a 下面插入
[root@tom ~]# sed ‘$a99999’ a.txt 文件最后一行下面增加内容
[root@tom ~]# sed ‘a99999’ a.txt 文件每行下面增加内容
[root@tom ~]# sed ‘5a99999’ a.txt 文件第5行下面增加内容
[root@tom ~]# sed ‘$i99999’ a.txt 文件最后一行上一行增加内容
[root@tom ~]# sed ‘i99999’ a.txt 文件每行上一行增加内容
[root@tom ~]# sed ‘6i99999’ a.txt 文件第6行上一行增加内容
[root@tom ~]# sed ‘/^bin/ihello’ 以bin开头行的上一行插入内容
[root@tom ~]# sed -n ‘/^bin/ihello’ a.txt 以bin开头行的上一行插入内容

3)修改文件内容
c 替换指定的整行内容
[root@tom ~]# sed ‘5chello world’ a.txt 替换文件第5行内容
[root@tom ~]# sed ‘chello world’ a.txt 替换文件所有内容
[root@tom ~]# sed ‘1,5chello world’ a.txt 替换文件1到5行内容为hello world
[root@tom ~]# sed ‘/^bin/c888888’ a.txt 替换以bin开头的行
4)删除文件内容
[root@tom ~]# sed ‘1d’ a.txt 删除文件第1行
[root@tom ~]# sed ‘1,5d’ a.txt 删除文件1到5行
[root@tom ~]# sed ‘$d’ a.txt 删除文件最后一行

② 对文件进行搜索替换操作
语法:sed 选项 ‘s/搜索的内容/替换的内容/动作’ 需要处理的文件
其中,s表示search搜索;斜杠/表示分隔符,可以自己定义;动作一般是打印p和全局替换g
[root@tom ~]# sed -n ‘s/root/ROOT/p’ a.txt
[root@tom ~]# sed -n ‘s/root/ROOT/gp’ a.txt
[root@tom ~]# sed -n ‘s/^#//gp’ a.txt
[root@tom ~]# sed -n ‘1,5s/^/#/p’ a.txt 注释掉文件的1-5行内容
[root@tom ~]# sed -n ‘s/sbin/nologin/xyz/gp’ a.txt
[root@tom ~]# sed -n ‘s@/sbin/nologin@xyz@gp’ a.txt – 自定义分隔符为@
注:当一个符号同时出现三次时 起到自定义分隔得作用
[root@tom ~]# sed -n ‘10s#/sbin/nologin#xyz#p’ a.txt --自定义分隔符为#
注意:搜索替换中的分隔符可以自己指定
③ 其他命令

举例说明:
r 从文件中读取输入行
w 将所选的行写入文件
& 保存查找串以便在替换串中引用 \ (\)
[root@tom ~]# sed ‘3r /etc/hosts’ a.txt
[root@tom ~]# sed ‘$r /etc/hosts’ a.txt
[root@tom ~]# sed ‘1,5w 11.txt’ 1.txt
[root@tom ~]# sed -n ‘s/^sync/#&/gp’ 1.txt
[root@tom ~]# sed -n ‘s/(^sync)/#\1/gp’ 1.txt

= 打印行号

[root@tom ~]# sed -ne ‘/root/p’ -ne ‘/root/=’ 1.txt
[root@tom ~]# sed -n ‘/root/p;/root/=’ 1.txt

! 对所选行以外的所有行应用命令,放到行数之后
[root@tom ~]# sed -n ‘1!p’ 1.txt
[root@tom ~]# sed -n ‘4p’ 1.txt
[root@tom ~]# sed -n ‘4!p’ 1.txt
[root@tom ~]# cat -n 1.txt
[root@tom ~]# sed -n ‘1,17p’ 1.txt
[root@tom ~]# sed -n ‘1,17!p’ 1.txt

q 退出

[root@tom ~]# sed ‘5q’ 1.txt

④ 其他选项
-e 多项编辑
-r 扩展正则
-i 修改原文件

[root@tom ~]# sed -ne ‘/root/p’ 1.txt -ne ‘/root/=’
root:\x:0:0:root:/root:/bin/bash
1

[root@tom ~]# sed -ne ‘/root/=’ -ne ‘/root/p’ 1.txt
1
root:\x:0:0:root:/root:/bin/bash

在1.txt文件中的第5行的前面插入“hello world”;在1.txt文件的第8行下面插入“哈哈哈哈”
[root@tom ~]# sed -e ‘5ihello world’ -e ‘8a哈哈哈哈哈’ 1.txt -e ‘5=;8=’
[root@tom ~]# sed -n ‘1,5p’ 1.txt
[root@tom ~]# sed -ne ‘1p’ -ne ‘5p’ 1.txt
[root@tom ~]# sed -ne ‘1p;5p’ 1.txt
[root@tom ~]# sed -nr ‘/([0-9]{1,3}.){3}[0-9]{1,3}/p’ 2.txt

-i 选项 直接修改原文件

[root@tom ~]# sed -i ‘s/root/ROOT/g’ a.txt

⑤ sed结合正则使用
sed 选项 'sed命令或者正则表达式或者地址定位 文件名
定址用于决定对哪些行进行编辑。地址的形式可以是数字、正则表达式、或二者的结合。
如果没有指定地址,sed将处理输入文件的所有行。

2. 脚本格式

脚本的第一行写上

#!/bin/sed -f
1,5d
s/root/hello/g
3i777
5i888
a999
p

㈠ 用法

# sed -f scripts.sh  file        //使用脚本处理文件
# ./sed.sh   file
sed -f 1.sed -i 1.txt

shell——sed工具相关推荐

  1. linux运维实战练习及linux shell脚本、awk、sed工具命令学习总结

    一.linux shell 脚本 1.描述shell程序的运行原理(可附带必要的图形说明): Linux系统的shell作为操纵系统的外壳,为用户提供使用操纵系统的接口.它是命令语言.命令解释程序及程 ...

  2. Shell编程之sed工具

    文章目录 sed工具概述 sed工作原理 sed工作 sed命令格式 sed 命令常见用法 sed命令的格式 sed命令的常用选项 编辑命令格式 sed的常用操作命令 常见的操作包括以下几种. sed ...

  3. shell 知识点补充(3)-修改语系/特殊字符/ printf/sed 工具/awk 工具/diff/cmp

    1.修改语系的方法为: [root@test root]# LANG=en              (根据情况指定为其它语法,如:C) [root@test root]# export LANG l ...

  4. linux中sed工具的简单解析与实例参考

    一.什么是sed sed是一种新型的,非交互式的编辑器.它没有提供交互式的使用方式,使用者只能在命令行输入编辑命令,指定文件名,然后在屏幕上查看输出.sed编辑器没有破坏习惯,它不会修改文件,除非使用 ...

  5. linux sed不起作用,Linux:sed工具

    1.什么是sed工具 sed意为流编辑器(Stream Editor),在Shell脚本和Makefile中作为过滤器使用非常普遍,也 就是 把前一个程序的输出引入sed的输入,经过一系列编辑命令转换 ...

  6. linux shell sed i,Linux Shell学习-sed命令详解

    (1).sed介绍 Sed是流编辑器,stream editor,它是一个将一些列编辑命令作用于一批文本文件的理想工具. (2).sed工作原理 Sed是一个非交互式文本编辑器,它可以对文本文件和标准 ...

  7. PHP Shell生成工具Weevely

    PHP Shell生成工具Weevely Weevely是一款模拟Telnet连接的PHP Shell工具.它不提供网页形式的接口,而是提供一个命令形式的终端.渗透测试人员首先使用该工具生成对应的PH ...

  8. linux shell sed awk 命令(2)-awk

    linux shell sed awk 命令(2)-awk awk语法格式: awk [选项] -f program-file [ -- ] file ... 选项: -F fs, --field-s ...

  9. Linux之Sed工具的使用详解

    Sed工具工作原理及特性 1. sed是流编辑器,每一次读取一行到内存中,即称之为模式空间(pattern space) 2. 默认不修改原文件,如果需要修改需加-i参数 3. sed有模式空间及保持 ...

最新文章

  1. 读后感与机翻《人类因果学习的分解:自下而上的联想学习和自上而下的图式推理》
  2. 【程序员基础篇】开源中国私有库代码更新
  3. morlet包络检波matlab,布里渊光纤传感系统中的信号处理的研究
  4. Jmeter之http性能测试实战 NON-GUI模式 进行分布式压力测试——干货(十二)(转载)...
  5. lintcode:最小编辑距离
  6. android记事本项目案例,Android实现记事本项目完整实例源代码
  7. P5713 【深基3.例5】洛谷团队系统(python3实现)
  8. 网友的VOIP总结 1
  9. 百度搜索大数据:“摆摊技巧”搜索热度暴涨655%;中国电信:将逐步关闭3G网络业务;IntelliJ IDEA新版发布|极客头条...
  10. 突发!Java首度“落泪”,愿永久祝福Python!网友:我也想哭!
  11. ios 仿微信,短信聊天气泡
  12. 蓝桥杯2015年第六届C/C++省赛B组第二题-星系炸弹
  13. Java-重载、重写(冷静分析)
  14. 看清贬值的真实情况再惊恐
  15. 嵌入式网络和数据库管理系统
  16. Harbor中镜像清理
  17. 计算机设备管理器被禁用,win系统管理员被禁用在安全模式的解决方法
  18. 十天内提高单词量到20000! (Vocabulary 10000)
  19. Bugku CTF 眼见为实(MISC)
  20. 如何使用bert做word embedding

热门文章

  1. excel通过sumproduct和countifs不重复计数(数据中包含空白单元)
  2. 以太坊网络区块浏览器blocksout技术简要分析
  3. 邮件服务器WinWebMail 3.7.1.1 企业版安装/注册机使用方法
  4. django 3.0
  5. 51单片机 DHT11+LCD1602温湿度显示 + Proteus仿真
  6. 自己动手写操作系统 将引导程序成功写入优盘启动电脑
  7. excel处理技巧三
  8. 为VMwareESXi添加驱动
  9. HTML5生日祝福网页代码
  10. Windows ❀ CMD中使用批量Ping网段命令教程