Shell 四剑客之 sed s替换内容以及n p

网友投稿 2388 2022-09-04

Shell 四剑客之 sed s替换内容以及n p

Shell 四剑客之 sed s替换内容以及n p

Vim 采用的是交互式文本编辑模式,你可以用键盘命令来交互性地插入、删除或替换数据中的文本。但 sed 命令不同,它采用的是流编辑模式,最明显的特点是,在 sed 处理数据之前,需要预先提供一组规则,sed 会按照此规则来编辑数据。 sed 会根据脚本命令来处理文本文件中的数据,这些命令要么从命令行中输入,要么存储在一个文本文件中,此命令执行数据的顺序如下:

每次仅读取一行内容;根据提供的规则命令匹配并修改数据。注意,sed 默认不会直接修改源文件数据,而是会将数据复制到缓冲区中,修改也仅限于缓冲区中的数据;将执行结果输出。

当一行数据匹配完成后,它会继续读取下一行数据,并重复这个过程,直到将文件中所有数据处理完毕。  sed 命令的基本格式如下:

[root@localhost ~]# sed [选项] [脚本命令] 文件名

该命令常用的选项及含义,如表所示:

sed 命令常用选项及含义

选项

含义

-e 脚本命令

该选项会将其后跟的脚本命令添加到已有的命令中。

-f 脚本命令文件

该选项会将其后文件中的脚本命令添加到已有的命令中。

-n

默认情况下,sed 会在所有的脚本指定执行完毕后,会自动输出处理后的内容,而该选项会屏蔽启动输出,需使用 print 命令来完成输出。

-i

此选项会直接修改源文件,要慎用。

成功使用 sed 命令的关键在于掌握各式各样的脚本命令及格式,它能帮你定制编辑文件的规则。

sed s 替换脚本命令

此命令的基本格式为:

[address]s/pattern/replacement/flags

其中,address 表示指定要操作的具体行,pattern 指的是需要替换的内容,replacement 指的是要替换的新内容。

此命令中常用的 flags 标记如表所示:

sed s命令flags标记及功能

flags 标记

功能

n

1~512 之间的数字,表示指定要替换的字符串出现第几次时才进行替换,例如,一行中有 3 个 A,但用户只想替换第二个 A,这是就用到这个标记;

g

对数据中所有匹配到的内容进行替换,如果没有 g,则只会在第一次匹配成功时做替换操作。例如,一行数据中有 3 个 A,则只会替换第一个 A;

p

会打印与替换命令中指定的模式匹配的行。此标记通常与 -n 选项一起使用。

w file

将缓冲区中的内容写到指定的 file 文件中;

&

用正则表达式匹配的内容进行替换;

\n

匹配第 n 个子串,该子串之前在 pattern 中用 \(\) 指定。

\

转义(转义替换部分包含:&、\ 等)。

sed替换内容实战

flags参数使用,表示指定要替换的字符串出现第几次时才进行替换

[root@localhost ~]# sed 's/test/trial/2' data4.txtThis is a test of the trial script.This is the second test of the trial script.可以看到,使用数字 2 作为标记的结果就是,sed 编辑器只替换每行中第 2 次出现的匹配模式。如果要用新文件替换所有匹配的字符串,可以使用 g 标记:[root@localhost ~]# sed 's/test/trial/g' data4.txtThis is a trial of the trial script.This is the second trial of the trial script.

提供ns来指定替换文本第几行的关键字内容 和 -e来将其后跟的脚本命令添加到已有的命令中

[root@~]# cat sed.txt 123qfqrootrootrootqwfokThis is a test of the trial script.This is the second test of the trial script.[root@~]# sed -e "1s/123/456/g" -e "3s/root/linux/g" sed.txt --使用-e和下面使用;号等价456qfqlinuxrootrootqwfokThis is a test of the trial script.This is the second test of the trial script.[root@~]# sed -e "1s/123/456/g;3s/root/linux/g" sed.txt 456qfqlinuxrootrootqwfokThis is a test of the trial script.This is the second test of the trial script.

sed N使用

[root@~]# cat list.txt 192.168.179.99192.168.179.100192.168.179.101192.168.179.102#sed以行为单位处理文本,其模式缓冲区内只保留一行内容待处理。N命令,将下一行读入并附加到当前行后面,以\n(换行符)分隔,一起存在模式缓冲区内\n 就表示换行符[root@~]# sed 'N;s/\n//g' list.txt -N表示开启多行处理,一次处理两行即将两行一次性读取到内存当中,并且将两行之间的换行符去掉192.168.179.99192.168.179.100192.168.179.101192.168.179.102[root@~]# sed 'N;N;s/\n//g' list.txt --以后每次多一个N就是多一行合并处理 192.168.179.99192.168.179.100192.168.179.101192.168.179.102[root@~]# sed = list.txt 1192.168.179.992192.168.179.1003192.168.179.1014192.168.179.102[root@~]# sed = list.txt | sed 'N;s/\n/)/g' --和上面一样一次处理两行将换行符换为)1)192.168.179.992)192.168.179.1003)192.168.179.1014)192.168.179.102

sed在某行前面后面添加内容

[root@~]# cat sed.sh 192.168.179.1.15192.168.179.1.16192.168.179.1.17[root@~]# sed '/192.168.179.1.15/s/^/---/g' sed.sh --在某一行前面添加-------192.168.179.1.15192.168.179.1.16192.168.179.1.17[root@~]# sed '/192.168.179.99/s/$/----/g' list.txt --在某一行末尾添加----192.168.179.99----192.168.179.100192.168.179.101192.168.179.102[root@~]# cat list.txt 192.168.179.99192.168.179.100192.168.179.101192.168.179.102[root@~]# sed '/192.168.179.99/s/99/999/g' list.txt --先匹配到某一行,再使用s替换192.168.179.999192.168.179.100192.168.179.101192.168.179.102[root@~]# sed -e '/192.168.179.99/s/^/---/g' -e '/192.168.179.99/s/$/---/g' list.txt --前面后面都添加------192.168.179.99---192.168.179.100192.168.179.101192.168.179.102[root@~]# sed -e 's/^/---/g' -e 's/$/---/g' list.txt --在所有的行前后添加信息---192.168.179.99------192.168.179.100------192.168.179.101------192.168.179.102---[root@~]# sed '/100/,/101/s/^/test/g' list.txt --在100-101行之间前面添加test192.168.179.99test192.168.179.100test192.168.179.101192.168.179.102[root@~]# nl /etc/passwd | head -n 5 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[root@~]# nl /etc/passwd | sed 's/^[[:space:]]*//g' | head -n 5 将前面的空格缩进1 root:x:0:0:root:/root:/bin/bash2 bin:x:1:1:bin:/bin:/sbin/nologin3 daemon:x:2:2:daemon:/sbin:/sbin/nologin4 adm:x:3:4:adm:/var/adm:/sbin/nologin5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin[root@~]# nl /etc/passwd | sed 's/^[[:space:]]*//g' | sed 's/\t/)/g' | head -n 51)root:x:0:0:root:/root:/bin/bash2)bin:x:1:1:bin:/bin:/sbin/nologin3)daemon:x:2:2:daemon:/sbin:/sbin/nologin4)adm:x:3:4:adm:/var/adm:/sbin/nologin5)lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin[root@~]# nl /etc/passwd | sed 's/^[[:space:]]*//g' | sed 's/\t/)/g' | sed 's/$/./g'| head -n 5 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.[root@~]# nl /etc/passwd | sed 's/^ *//g' | sed 's/\t/)/g' | sed 's/$/./g' | head -n 51)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.

sed n p(一般和sed s替换一起使用 sed -n 's/pattern/replacement/p')

我们知道,-n 选项会禁止 sed 输出,但 p 标记会输出修改过的行,将二者匹配使用的效果就是只输出被替换命令修改过的行,例如:

[root@localhost ~]# cat data5.txtThis is a test line.This is a different line.[root@localhost ~]# sed -n 's/test/trial/p' data5.txtThis is a trial line.[root@~]# sed -n '/nginx/p' /usr/local/nginx/conf/nginx.confuser nginx;pid logs/nginx.pid;[root@~]# sed -n 's/nginx/test/p' /usr/local/nginx/conf/nginx.confuser test;pid logs/test.pid;

打印匹配区间范围的行

[root@~]# sed -e '/#/d' -e '/^$/d' -e '/server/,$d' nginx.conf | sed -n '/events/,/{ worker_connections 1024;}{

下面再随便举几个例子:

数据的搜寻并显示

搜索 /etc/passwd有root关键字的行

nl /etc/passwd | sed '/root/p'1 root:x:0:0:root:/root:/bin/bash2 daemon:x:1:1:daemon:/usr/sbin:/bin/sh3 bin:x:2:2:bin:/bin:/bin/sh4 sys:x:3:3:sys:/dev:/bin/sh5 sync:x:4:65534:sync:/bin:/bin/sync....下面忽略

如果root找到,除了输出所有行,还会输出匹配行。使用-n的时候将只打印包含模板的行。

nl /etc/passwd | sed -n '/root/p'1 root:x:0:0:root:/root:/bin/bash

[root@~]# cat sed.txt 123qfqrootrootrootqwfokThis is a test of the trial script.This is the second test of the trial script.[root@~]# sed "/root/p"sed.txt 123qfqrootrootrootrootrootrootqwfokThis is a test of the trial script.This is the second test of the trial script.[root@~]# sed -n "/root/p" sed.txt --查找某一行的关键字使用-n p选项,n是静默,p是打印rootrootroot[root@~]# sed -n 's/disabled/enforcing/p' /etc/selinux/config # enforcing - No SELinux policy is loaded.SELINUX=enforcing[root@~]# sed -n "/qwf/"p sed.txt --查看某一行的信息qwf[root@~]# sed -n "1p" sed.txt 123[root@~]# sed -n "2p" sed.txt qfq[root@~]# sed -n "1,2p" sed.txt --查看1-2行 123qfq[root@~]# sed -n "1p;3p" sed.txt --查看第1行第3行123root

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:PHP中traits的作用和使用(php trait 和 公共函数的区别)
下一篇:ELK logstash json过滤器
相关文章

 发表评论

暂时没有评论,来抢沙发吧~