当前位置: 首页 > news >正文

Linux处理文件sed

一、什么是sed

	sed全称(stream editor)流式编辑器,Sed主要用来自动编辑一个或多个文件、简化对文件的反复操作、编写转换程序等,工作流程如下sed 是一种在线的、非交互式的编辑器,它一次处理一行内容。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。文件内容并没有改变,除非你使用重定向存储输出,或者使用sed -i选项-i选项就是将本该输出到屏幕上的内容输出/流入文件中

sed命令格式如下

sed [options] 'command' file(s)
sed [options] -f scriptfile file(s)
注意:
sed和grep不一样,不管是否找到指定的模式,它的退出状态都是0
只有当命令存在语法错误时,sed的退出状态才不是0

二. 作用

作用: 可以直接编辑修改文件内容,可以对文件进行增删改查
语法:
sed 参数 “[定位 指令]” 文件路径
参数:

	  -e 允许多项编辑-r 扩展正则-n 取消默认输出(模式空间的内容输出)-i 写入文件  改变了输出流向,对源文件修改 (把流向屏幕的内容,流入文件中)-i.bak 修改文件时会创建备份文件,防止手误-a 指定行后添加信息-i 指定行前添加信息-f 指定sed脚本文件名

指令:

p 打印到屏幕
d 删除
I 忽略大小写
!表示取反
s/// 替换
e 识别多条指令 
c将整行内容清除后替换为c后面指定的内容
指令可以用;号连接多条,如1d;3d;5d代表删除1,3,5

三. 定位

1. 行定位

a.打印/etc/passwd文件的第10行

[root@manager ~]# sed -n '10p' /etc/passwd

b.打印第5行到第10行

[root@manager ~]# sed -n '5,10p' /etc/passwd

c.从第5行开始,往后打印5行,包括第5行

[root@manager ~]# sed -n '5,+5p' /etc/passwd

d.打印/etc/passwd中开头为root的行开始,到开头为ftp的行结束的内容

[root@manager ~]# sed -n '/^root/,/ftp/p' /etc/passwdroot:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologinlp:x:4:7:lp:/var/spool/lpd:/sbin/nologinsync:x:5:0:sync:/sbin:/bin/syncshutdown:x:6:0:shutdown:/sbin:/sbin/shutdownhalt:x:7:0:halt:/sbin:/sbin/haltmail:x:8:12:mail:/var/spool/mail:/sbin/nologinoperator:x:11:0:operator:/root:/sbin/nologingames:x:12:100:games:/usr/games:/sbin/nologinftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

e.打印/etc/passwd中第8行开始,到含有/sbin/nologin的内容的行结束内容

[root@manager ~]# sed -n '8,/sbin\/nologin/p' /etc/passwd

f.取第七行或十二行

[root@localhost ~]# sed -n "7p;12p" /etc/selinux/config SELINUX=enforcingSELINUXTYPE=targeted 

2.正则表达式定位

与grep一样,sed在文件中查找模式时也可以使用正则表达式(RE)和各种元字符。正则表达式是
括在斜杠间(/xxx/)的模式,用于查找和替换,以下是sed支持的元字符。

	# 使用基本元字符集	^, $, ., *, [], [^], \< \>,\(\),\{\}# 使用扩展元字符集	?, +, { }, |, ( )# 使用扩展元字符的方式:转义,如\+-r参数,如sed -r

示例:
“/love/” 包含love的行
“/^love/” 以love开头的行
“/love$/” 以love结尾的行
a.根据行号筛查文件信息:
p打印

[root@localhost ~]# sed -n "/root/p" /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

b.根据内容筛选文件信息-
取消selinux 配置文件的空行和注释行

[root@localhost ~]# sed -rn '/^#|^$/!p' /etc/selinux/config 
SELINUX=enforcing
SELINUXTYPE=targeted 
[root@localhost ~]#cat /etc/selinux/config| egrep -v '^$|^#'
SELINUX=enforcing
SELINUXTYPE=targete
[root@localhost ~]# cat -n /etc/selinux/config 1	2	# This file controls the state of SELinux on the system.3	# SELINUX= can take one of these three values:4	#     enforcing - SELinux security policy is enforced.5	#     permissive - SELinux prints warnings instead of enforcing.6	#     disabled - No SELinux policy is loaded.7	SELINUX=enforcing8	# SELINUXTYPE= can take one of three values:9	#     targeted - Targeted processes are protected,10	#     minimum - Modification of targeted policy. Only selected processes are protected. 11	#     mls - Multi Level Security protection.12	SELINUXTYPE=targeted 

c.打印jfedu.txt文本中第一行与最后一行

sed    -n '1p;$p'           jfedu.txt

d. 删除jfedu.txt第一行至第三行、删除匹配行至最后一行

sed       '1,3d'             jfedu.txt
sed       '/jfedu/,$d'         jfedu.txt

e. 删除jfedu.txt最后一行

sed       '$d'             jfedu.txt

f.在jfedu.txt查找jfedu所在行,并在其下一行添加word字符,a表示在其下一行添加字符串:

sed    '/jfedu/aword'      jfedu.txt

g. 在jfedu.txt查找jfedu所在行,并在其上一行添加word字符,i表示在其上一行添加字符串

sed    '/jfedu/iword'       jfedu.txt

四。增加编辑命令

命令 	含义
a	    行后追加内容 append
i	    行前追加内容 insert
r	    读入外部文件,行后追加
w	    将匹配行写入外部文件

1. 行号a (append)

[root@localhost ~]# sed "1ahello" a.txt111hello222333
[root@localhost ~]# sed "2ahello" a.txt
111
222
hello
333
[root@localhost ~]# sed "1,2ahello" a.txt # 第一行到第三行,每行后都增加hello
111
hello
222
hello
333
# 第一行和第三行后添加hello
[root@manager ~]# sed -e '1ahello' -e '3ahello' a.txt 
111
hello
222
333
hello[root@localhost ~]# sed '$a hello' a.txt # 在最后一行末尾添加 用单引号
111
222
333
hello
#将/etc/fstab文件的内容追加到a.txt文件的第2行后面
[root@manager ~]# sed '2r /etc/fstab' a.txt # 不能加a#将/etc/inittab文件内容追加到/etc/passwd文件匹配/bin/sync行的后面
sed '/\/bin\/rsync/r /etc/inittab' /etc/passwd#将passwd文件匹配到/bin/bash的行追加到/tmp/a.txt文件中
[root@manager ~]# sed  '/\/bin\/bash/w /tmp/a.txt' /etc/passwd

在指定行前插入信息-行号i (insert)

[root@localhost ~]# sed "1ihello" a.txthello111222333
方法1:
## 在第一行后面添加两行hello world 
[root@localhost ~]# sed "1ahello\nworld" a.txt 
111
hello
world
222
333
方式2[root@localhost ~]# sed -e "1ahello" -e "1a world" a.txt
111
hello
world
222
333

五. 修改文件信息

命令	含义
1s/old/new/	替换第1行内容old为new
1,10s/old/new/	替换1行到10行的内容old为new
1,+5s/old/new/	替换1行到6行的内容old为new
/pattern1/s/old/new/	替换匹配pattern1的内容old为new
/pattern1/,/pattern2/s/old/new/	替换匹配到pattern1的行直到
匹配到pattern2的所有行内容old为new
10,/pattern1/s/old/new/	替换第10行直到匹配到pattern1的所有行内容old为new

有如下文件内容

[root@localhost ~]# cat a.txt
aaa
bbb
ccc
aaa hello aaa AAA
  1. 匹配到aaa替换为AAA, 默认只匹配每行的第一次出现
[root@localhost ~]# sed "s/aaa/AAA/" a.txt
AAA
bbb
ccc
AAA hello aaa AAA
world bbb bbb
ddd
  1. 替换每行的所有aaa为AAA g参数
[root@localhost ~]# sed "s/aaa/AAA/g" a.txt
AAA
bbb
ccc
AAA hello AAA AAA
world bbb bbb
ddd
  1. 替换指定行,例如只替换第四行
[root@localhost ~]# sed "4s/aaa/AAA/g" a.txt
aaa
bbb
ccc
AAA hello AAA AAA
world bbb bbb
ddd
  1. 替换第一行到第四行
[root@localhost ~]# sed "1,4s/aaa/AAA/g" a.txt
AAA
bbb
ccc
AAA hello AAA AAA
world bbb bbb
ddd
  1. 替换第一行的aaa,或第五行的bbb
[root@localhost ~]# sed "1s/aaa/AAA/g;5s/bbb/BBB/g" a.txt
AAA
bbb
ccc
aaa hello aaa AAA
world BBB BBB
ddd
  1. 在jfedu.txt查找www的行,在其行首添加字符串word,^表示起始标识,&在Sed中表示添加:
sed   '/www/s/^/&word/'    jfedu.txt
  1. Sed读取系统变量,变量替换:
WEBSITE=WWW.JFEDU.NET
Sed  “s/www.jd.com/$WEBSITE/g” jfedu.txt
  1. 修改Selinux策略enforcing为disabled,查找/SELINUX/行,然后将其行enforcing值改成disabled、!s表示不包括SELINUX行:
sed  -i   '/SELINUX/s/enforcing/disabled/g' /etc/selinux/config
sed  -i   '/SELINUX/!s/enforcing/disabled/g' /etc/selinux/config

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • Java 新手学习线路,Java 学习路线是怎样的?
  • uniapp自定义tabBar
  • unity2022 il2cpp 源码编译
  • 信息检索(39):Condenser: a Pre-training Architecture for Dense Retrieval
  • SpringBoot源码深度解析
  • # Redis 入门到精通(九)-- 主从复制(1)
  • buu做题(6)
  • 时间卷积网络(TCN):序列建模的强大工具(附Pytorch网络模型代码)
  • 入门 git
  • MySQL:SELECT 语句
  • Android 11 HAL层集成FFMPEG
  • Flink源码学习资料
  • 机体坐标系和导航坐标系
  • 【中项】系统集成项目管理工程师-第2章 信息技术发展-2.1信息技术及其发展-2.1.1计算机软硬件与2.1.2计算机网络
  • springboot防止重复提交的方案有哪些
  • 【140天】尚学堂高淇Java300集视频精华笔记(86-87)
  • 【跃迁之路】【463天】刻意练习系列222(2018.05.14)
  • happypack两次报错的问题
  • iOS帅气加载动画、通知视图、红包助手、引导页、导航栏、朋友圈、小游戏等效果源码...
  • JavaScript标准库系列——Math对象和Date对象(二)
  • Joomla 2.x, 3.x useful code cheatsheet
  • Python 反序列化安全问题(二)
  • Python进阶细节
  • Quartz初级教程
  • REST架构的思考
  • Sequelize 中文文档 v4 - Getting started - 入门
  • Vue ES6 Jade Scss Webpack Gulp
  • 得到一个数组中任意X个元素的所有组合 即C(n,m)
  • 构建工具 - 收藏集 - 掘金
  • 基于axios的vue插件,让http请求更简单
  • 类orAPI - 收藏集 - 掘金
  • 前端攻城师
  • 前端面试之闭包
  • ​​​​​​​​​​​​​​Γ函数
  • ​520就是要宠粉,你的心头书我买单
  • ​Linux Ubuntu环境下使用docker构建spark运行环境(超级详细)
  • #ifdef 的技巧用法
  • (145)光线追踪距离场柔和阴影
  • (C)一些题4
  • (C语言)求出1,2,5三个数不同个数组合为100的组合个数
  • (MIT博士)林达华老师-概率模型与计算机视觉”
  • (pojstep1.1.2)2654(直叙式模拟)
  • (三) prometheus + grafana + alertmanager 配置Redis监控
  • (三)终结任务
  • (四)Controller接口控制器详解(三)
  • (五)IO流之ByteArrayInput/OutputStream
  • (原创)攻击方式学习之(4) - 拒绝服务(DOS/DDOS/DRDOS)
  • (转载)OpenStack Hacker养成指南
  • .net 8 发布了,试下微软最近强推的MAUI
  • .net反编译工具
  • .Net高阶异常处理第二篇~~ dump进阶之MiniDumpWriter
  • .Net面试题4
  • .NET设计模式(8):适配器模式(Adapter Pattern)
  • .net实现头像缩放截取功能 -----转载自accp教程网
  • .net中调用windows performance记录性能信息