2019-03-13笔记—shell脚本编程四(sed)

sed命令主要用于数据的查找替换。

PS:一般对于grep和sed命令,如果对于区分正则不熟悉的情况下,可以直接使用“egrep”和“sed -r”命令

命令参数:

选项 作用
-n 把经过 sed 命令处理的行输出到屏幕上,默认sed 命令会把所有数据都输出到屏幕上
-e 允许对输入数据应用多条 sed 命令编辑;
-r 在 sed 中支持扩展正则表达式;
-i 用 sed 的修改结果直接修改读取数据的文件,而不是由屏幕输出动作;
-I 不区分大小写,grep命令参数是(grep -i)
d 删除指定的行;
P 打印输出指定的行;
s 字符串替换,用一个字符串替换另一个字符串。格式为“行范围s/旧字串/新字串/g”(和Vim中的替换格式类似);
a \ 追加,在当前行后添加一行或多行。当添加多行时,除最后一行外,每行末尾需要用“\”代表数据未完结;
c \ 行替换,用c后面的字符串替换原数据行。当替换多行时,除最后一行外,每行末尾需用“\”代表数据未完结;
i \ 插入,在当前行前插入一行或多行。当插入多行时,除最后一行外,每行末尾需要用“\”代表数据未完结;

建立测试文件

[root@linux2019 shell]# cat -n test.txt 
     1	Mr.Duan Ming said:
     2	he was the most honest man in School.
     3	123despise him.
     4	
     5	But since Mr.Zhang Shan came,
     6	he never saaaid those words.
     7	5555nice!
     8	
     9	because,actuaaaally,
    10	Mr.Duan Si is the most honest man Later,Mr.Duan ming soid his hot body.
[root@linux2019 shell]# sed -n '2,5'p  test.txt #打印2至5行
he was the most honest man in School.
123despise him.

But since Mr.Zhang Shan came,
[root@linux2019 shell]# sed -n '7,$'p  test.txt #打印从第七行到末行
5555nice!

because,actuaaaally,
Mr.Duan Si is the most honest man Later,Mr.Duan ming soid his hot body.
[root@linux2019 shell]# sed  -n '/the/'p  test.txt #打印包含“the”字符串的行
he was the most honest man in School.
Mr.Duan Si is the most honest man Later,Mr.Duan ming soid his hot body.
[root@linux2019 shell]# sed  -n '/^[0-9]/'p  test.txt #打印数字开头的行
123despise him.
5555nice!
[root@linux2019 shell]# sed  -n '/^[^0-9]/'p  test.txt #打印不以数字开头的行,以下等同
Mr.Duan Ming said:
he was the most honest man in School.
But since Mr.Zhang Shan came,
he never saaaid those words.
because,actuaaaally,
Mr.Duan Si is the most honest man Later,Mr.Duan ming soid his hot body.
[root@linux2019 shell]# sed  -n '/^[a-zA-Z]/'p  test.txt 
Mr.Duan Ming said:
he was the most honest man in School.
But since Mr.Zhang Shan came,
he never saaaid those words.
because,actuaaaally,
Mr.Duan Si is the most honest man Later,Mr.Duan ming soid his hot body.
[root@linux2019 shell]# sed -e '1'p -e '/nice/'p  -n test.txt   #打印第一行和包含“nice”的行
Mr.Duan Ming said:
5555nice!
[root@linux2019 shell]# sed '7,$'d test.txt 打印删除了从第七行到末行的结果
Mr.Duan Ming said:
he was the most honest man in School.
123despise him.

But since Mr.Zhang Shan came,
he never saaaid those words.
[root@linux2019 shell]# sed -e '2,7s/he/she/g' -e '8,$'d  test.txt #替换2-7行的he成she,并删除7-10行
Mr.Duan Ming said:
she was tshe most honest man in School.
123despise him.

But since Mr.Zhang Shan came,
she never saaaid those words.
5555nice!
[root@linux2019 shell]# sed 's/^.*$/&123/' test.txt #在所有行后面加123后打印
Mr.Duan Ming said:123
he was the most honest man in School.123
123despise him.123
123
But since Mr.Zhang Shan came,123
he never saaaid those words.123
5555nice!123
123
because,actuaaaally,123
Mr.Duan Si is the most honest man Later,Mr.Duan ming soid his hot body.123
[root@linux2019 shell]#  sed -r -e  's/(zabbix)(.*)(nologin)/\3\2\1/g' -e '$'p -n  passwd   #快位置调整
nologin:x:996:993:Zabbix Monitoring System:/var/lib/zabbix:/sbin/zabbix
[root@linux2019 shell]# sed -i 's/he/she/g' test.txt #-i直接写入文件
[root@linux2019 shell]# cat test.txt 
Mr.Duan Ming said:
she was tshe most honest man in School.
123despise him.

But since Mr.Zhang Shan came,
she never saaaid those words.
5555nice!

because,actuaaaally,
Mr.Duan Si is tshe most honest man Later,Mr.Duan ming soid his hot body.
[root@linux2019 shell]# sed  -e '3c abcd123abcd'p -e '1,5'p -n   test.txt #行替换
Mr.Duan Ming said:
he was the most honest man in School.
abcd123abcdp

But since Mr.Zhang Shan came,

猜你喜欢

转载自blog.csdn.net/ai_benwoniu/article/details/88537476