Linux基础命令-sed流编辑器

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, --silent

              suppress automatic printing of pattern space

       -e script, --expression=script

              add the script to the commands to be executed

       -f script-file, --file=script-file

              add the contents of script-file to the commands to be executed

       --follow-symlinks

              follow symlinks when processing in place

       -i[SUFFIX], --in-place[=SUFFIX]

              edit files in place (makes backup if SUFFIX supplied)

       -c, --copy

              use copy instead of rename when shuffling files in -i mode

       -b, --binary

              does  nothing;  for  compatibility  with  WIN32/CYGWIN/MSDOS/EMX  ( open files in
              binary mode (CR+LFs are not treated specially))

       -l N, --line-length=N
              --posix

              disable all GNU extensions.

       -r, --regexp-extended

              use extended regular expressions in the script.

       -s, --separate

              consider files as separate rather than as a single continuous long stream.

       -u, --unbuffered

              load minimal amounts of data from the input files and flush  the  output  buffers
              more often

       -z, --null-data

              separate lines by NUL characters

       --help

              display this help and exit

       --version

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

猜你喜欢

转载自blog.csdn.net/rhn_111/article/details/129466321
今日推荐