Linux基础命令-sed流编辑器
创始人
2024-06-01 17:26:16
0

Linux三剑客-grep命令

Sed

一. 命令介绍

先到帮助文档中查看命令的描述信息

NAME
sed - stream editor for filtering and transforming text

sed命令是操作、过滤和转换文本内容的强大工具,常用功能包括结合正则表达式对文件实现快速增删改查,其中查询的功能中最常用的两大功能是过滤(提取字符串)以及 取行(取出指定行)

小贴士:注意sed和awk都是使用单引号,双引号有特殊解释。

二. 语法格式

语法格式是:sed 【参数】 【sed内置命令字符】【文件】

SYNOPSIS
sed [OPTION]… {script-only-if-no-other-script} [input-file]…

三. 基本参数

       -n, --quiet, --silentsuppress automatic printing of pattern space-e script, --expression=scriptadd the script to the commands to be executed-f script-file, --file=script-fileadd the contents of script-file to the commands to be executed--follow-symlinksfollow symlinks when processing in place-i[SUFFIX], --in-place[=SUFFIX]edit files in place (makes backup if SUFFIX supplied)-c, --copyuse copy instead of rename when shuffling files in -i mode-b, --binarydoes  nothing;  for  compatibility  with  WIN32/CYGWIN/MSDOS/EMX  ( open files inbinary mode (CR+LFs are not treated specially))-l N, --line-length=N--posixdisable all GNU extensions.-r, --regexp-extendeduse extended regular expressions in the script.-s, --separateconsider files as separate rather than as a single continuous long stream.-u, --unbufferedload minimal amounts of data from the input files and flush  the  output  buffersmore often-z, --null-dataseparate lines by NUL characters--helpdisplay this help and exit--versionoutput version information and exit

常用的命令参数有以下这些:

-n只输出匹配到的内容,常与sed内置命令p一起使用
-i直接将修改的结果内容写入文件
-e多次编辑
-r支持正则表达式扩展

如果不加-i参数,表示sed命令仅在内存中执行此命令,文件内的信息并不会发生变化;加上-i参数后,就会将结果写入到文件内,不输出到屏幕上。

sed内置命令参数:

a追加内容,在指定行后面添加一行或多行文本
d删除匹配行
i表示插入文本,在指定行前面添加一行或多行
p打印匹配行的内容,通常与-n一起使用
s/正则/替换内容/g匹配正则内容,然后替换内容,结尾g代表全局匹配

s/正则/替换内容/g ,中间的/可以换成其他任意字符,可以换成#号、@号、+号等等。

sed匹配的范围:

空地址:表示全文处理
单地址:指定文件某一行
/pattern/:被模式匹配到的每一行
范围区间:10,20 表示10到20行,10,+5表示第10行向下5行内容,/pattern1/,/pattern2/
步长:1-2,表示1、3、5、7、9行 基数行,2-2两个步长,表示2、4、6、8、10 偶数行

四. 参考案例

4.1 查找文件内容

查找/etc/passwd 第二行到第五行的内容,-n p表示将匹配的结果输出到屏幕上

[root@localhost ~]# sed -n "2,5p" /etc/passwd
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

查找/etc/passwd第五行往下三行的内容

[root@localhost ~]# sed -n "5,+3p" /etc/passwd
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt

4.2 查找关键字的内容

/etc/passwd中查找过滤带有root字符的内容,并且打印到屏幕上

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

4.3 删除文件的行

删除带有good的行,这里会需要用到-i的参数,会看到并没有输出结果到屏幕,而是直接将要执行的结果写入文件。

[root@localhost ~]# sed "/good/d" test.txt -i
[root@localhost ~]# cat test.txt 
appleeeeeee
bear
appdieee
dadaaaaaa
pperd

还可删除某一行到结尾的内容

[root@localhost ~]# sed '3,$d' test.txt -i
[root@localhost ~]# cat test.txt 
appleeeeeee
bear

如果从root到ftp之间的文本都要删除,但不知道文件的文本行号的多少,那么可以使用正则来执行此任务。

[root@localhost ~]# sed '/^root/,/^ftp/d' passwd | head -10
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
host:x:1000:1000:host:/home/host:/bin/bash
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
libstoragemgmt:x:998:994:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin
qemu:x:107:107:qemu user:/:/sbin/nologin
#执行的结果到文件中
[root@localhost ~]# sed '/^root/,/^ftp/d' passwd -i

4.4 替换文本内容

替换文本内容就需要用到s///g的参数,当然可以使用这个方法将结果写入到文件里。

[root@localhost ~]# sed 's/bear/pear/g' test.txt -i
[root@localhost ~]# cat test.txt 
appleeeeeee
pear
appleeeeeee
appleeeeeee
pear
pear
appleeeeeee
pear
[root@localhost ~]# sed 's#apple#door#g' test.txt
dooreeeeee
pear
dooreeeeee
dooreeeeee
pear
pear
dooreeeeee
pear

还可以多次替换文本的内容

[root@localhost ~]# sed -e 's/do/done/g' -e 's/pear/cat/g' test.txt -i
[root@localhost ~]# cat test.txt 
appleeeeeee
cat
appleeeeeee
appleeeeeee
cat
cat
appleeeeeee
cat

4.5 写入内容

追加内容到下一行,并且写入到文件中

[root@localhost ~]# sed '3a hello world!' passwd -i
[root@localhost ~]# head -n 5 passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
hello world!
adm:x:3:4:adm:/var/adm:/sbin/nologin

如果想在每一行后都添加内容,可以不用在前面加数字

[root@localhost ~]# sed 'a hello world' passwd -i 
[root@localhost ~]# head -5 passwd
root:x:0:0:root:/root:/bin/bash
hello world
bin:x:1:1:bin:/bin:/sbin/nologin
hello world

还可以同时写入两行内容,使用\n进行换行

[root@localhost ~]# sed '5a this is passwd\nthis is test' passwd -i
[root@localhost ~]# sed '1a this is passwd\nthis is test' passwd -i
[root@localhost ~]# head -5 passwd
root:x:0:0:root:/root:/bin/bash
this is passwd
this is test
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin

4.6 查看网卡的IP地址

这里使用-e的用法,也可以说是使用了掐头去尾法,使用了三种sed截取的用法。

[root@localhost ~]# ifconfig ens32 | sed '2p' -n | sed 's/^.*inet//' | sed 's/net.*$//' 192.168.10.24  [root@localhost ~]# ifconfig ens32 | sed -e '2s/^.*inet//'  -e '2s/net.*$//p' -n 192.168.10.24  [root@localhost ~]# ifconfig ens32 |sed '2s/^.*inet//;s/netmask.*//p' -n 

4.7 查看配置文件

我们想查看配置文件中不包含带有#号的文件有哪些内容,是不是也可以用到这个命令呢,可以删除带有#号和空格的行,这样显示出来的就是想看到的配置文件的信息.

[root@localhost ~]# sed -e '/#/d' -e '/^$/d' /etc/httpd/conf/httpd.conf 

不使用-e参数也是没问题的,可以使用分号将两个匹配的信息写在一起。

[root@localhost ~]# sed -e '/#/d;/^$/d' /etc/httpd/conf/httpd.conf 

4.8 给1-3行的内容前面添加#号

一般情况下内容前面是没有空格的,如果做到这个效果?其实这里一样是需要用到替换的方式去实现。
用到-r参数扩展正则,匹配第一个字符信息放入第一个组,第二个组写上#号,并且使用\1引用前一个分组的结果进行匹配。

[root@localhost ~]# sed -r -n  '1,3s/(^.)/#\1/gp' passwd
#root:x:0:0:root:/root:/bin/bash
#bin:x:1:1:bin:/bin:/sbin/nologin
#daemon:x:2:2:daemon:/sbin:/sbin/nologin

4.9 找出系统版本

可以先查看这个文件的信息

[root@localhost ~]# cat /etc/redhat-release 
CentOS Linux release 7.9.2009 (Core)

如果我只想查看具体的版本信息,比如7.9.2009,使用awk的话会比较简单。

[root@localhost ~]# awk '{print $4}' /etc/redhat-release 
7.9.2009

若要使用sed命令,该如何使用,我们先想一个思路,是不是可以像之前截取IP一样,使用掐头去尾的方法

[root@localhost ~]# sed -r -n 's/^.*ase[[:space:]]//p' /etc/redhat-release 
7.9.2009 (Core)

总结

sed的功能真的很强大,用来过滤、转换文本,或写入信息,都可以选择使用这个参数命令,如果觉得还行的,可以点赞支持一下!
在这里插入图片描述

相关内容

热门资讯

电视安卓系统哪个品牌好,哪家品... 你有没有想过,家里的电视是不是该升级换代了呢?现在市面上电视品牌琳琅满目,各种操作系统也是让人眼花缭...
安卓会员管理系统怎么用,提升服... 你有没有想过,手机里那些你爱不释手的APP,背后其实有个强大的会员管理系统在默默支持呢?没错,就是那...
安卓系统软件使用技巧,解锁软件... 你有没有发现,用安卓手机的时候,总有一些小技巧能让你玩得更溜?别小看了这些小细节,它们可是能让你的手...
安卓系统提示音替换 你知道吗?手机里那个时不时响起的提示音,有时候真的能让人心情大好,有时候又让人抓狂不已。今天,就让我...
安卓开机不了系统更新 手机突然开不了机,系统更新还卡在那里,这可真是让人头疼的问题啊!你是不是也遇到了这种情况?别急,今天...
安卓系统中微信视频,安卓系统下... 你有没有发现,现在用手机聊天,视频通话简直成了标配!尤其是咱们安卓系统的小伙伴们,微信视频功能更是用...
安卓系统是服务器,服务器端的智... 你知道吗?在科技的世界里,安卓系统可是个超级明星呢!它不仅仅是个手机操作系统,竟然还能成为服务器的得...
pc电脑安卓系统下载软件,轻松... 你有没有想过,你的PC电脑上安装了安卓系统,是不是瞬间觉得世界都大不一样了呢?没错,就是那种“一机在...
电影院购票系统安卓,便捷观影新... 你有没有想过,在繁忙的生活中,一部好电影就像是一剂强心针,能瞬间让你放松心情?而我今天要和你分享的,...
安卓系统可以写程序? 你有没有想过,安卓系统竟然也能写程序呢?没错,你没听错!这个我们日常使用的智能手机操作系统,竟然有着...
安卓系统架构书籍推荐,权威书籍... 你有没有想过,想要深入了解安卓系统架构,却不知道从何下手?别急,今天我就要给你推荐几本超级实用的书籍...
安卓系统看到的炸弹,技术解析与... 安卓系统看到的炸弹——揭秘手机中的隐形威胁在数字化时代,智能手机已经成为我们生活中不可或缺的一部分。...
鸿蒙系统有安卓文件,畅享多平台... 你知道吗?最近在科技圈里,有个大新闻可是闹得沸沸扬扬的,那就是鸿蒙系统竟然有了安卓文件!是不是觉得有...
宝马安卓车机系统切换,驾驭未来... 你有没有发现,现在的汽车越来越智能了?尤其是那些豪华品牌,比如宝马,它们的内饰里那个大屏幕,简直就像...
p30退回安卓系统 你有没有听说最近P30的用户们都在忙活一件大事?没错,就是他们的手机要退回安卓系统啦!这可不是一个简...
oppoa57安卓原生系统,原... 你有没有发现,最近OPPO A57这款手机在安卓原生系统上的表现真是让人眼前一亮呢?今天,就让我带你...
安卓系统输入法联想,安卓系统输... 你有没有发现,手机上的输入法真的是个神奇的小助手呢?尤其是安卓系统的输入法,简直就是智能生活的点睛之...
怎么进入安卓刷机系统,安卓刷机... 亲爱的手机控们,你是否曾对安卓手机的刷机系统充满好奇?想要解锁手机潜能,体验全新的系统魅力?别急,今...
安卓系统程序有病毒 你知道吗?在这个数字化时代,手机已经成了我们生活中不可或缺的好伙伴。但是,你知道吗?即使是安卓系统,...
奥迪中控安卓系统下载,畅享智能... 你有没有发现,现在汽车的中控系统越来越智能了?尤其是奥迪这种豪华品牌,他们的中控系统简直就是科技与艺...