三剑客sed命令

动 作解释
a新增a 的后面可以接字串,而这些字串会在新的一行出现(目前的下一行)~
c取代c 的后面可以接字串,这些字串可以取代 n1,n2 之间的行!
d删除因为是删除啊,所以 d 后面通常不接任何咚咚;
i插入i 的后面可以接字串,而这些字串会在新的一行出现(目前的上一行);
p打印亦即将某个选择的数据印出。通常 p 会与参数 sed -n 一起运行~
s取代可以直接进行取代的工作哩!通常这个 s 的动作可以搭配正规表示法!例如 1,20s/old/new/g 就是啦!

^开头 $结尾
加个!转义词转义就是不显示空

在这里插入图片描述
在这里插入图片描述

sed特殊正则

sed里有些正则和普通的正则语法不太一样,下面列出一些sed特殊语法的正则。

\+		#匹配一次或多次  
\?		#匹配零次或一次
\|		#或语法

查看文件某一行

先用seq生成序列

[root@shell ~ 48]# seq 5
1
2
3
4
5
###seq的作用
##生成50个数字序列重定向到文件中
[root@shell ~ 49]# seq 50 > zheng.txt 

显示第一行,-n用来取消默认输出,不加-n 要不然sed默认显示文件所有内容
p是打印 print的意思

[root@shell ~ 55]# sed -n '3p' zheng.txt 
3

查看文件几行到几行

用逗号隔开数字,p在最后

[root@shell ~ 56]# sed -n '10,15p' zheng.txt 
10
11
12
13
14
15

查看文件的好多行

可以看到,这里显示行号空格也算行,但是用nl显示空行不显示行号
,所以用cat -n或者grep -n

[root@zheng /]# nl /zyf/jianli.txt 
     1  I am student!
     2  I like Liunx.
       
     3  I like basketball
     4  I like badminton ball ,billiard ball and chinese chess!
     5  my blog is http://oldboy.blog.51cto.com
     6  our size is http://blog.oldoyedu.com
     7  2020-08-18
     8  my WeChat is 13522828457
       
     9  not 490000448.
    10  my god ,i an not oldbey,but OlDEBOY!
    11  ....******!!!!!!!|||||||#####^^^^^
    12  2020-08-20
[root@zheng /]# sed -n '1p;8p;11p' /zyf/jianli.txt
I am student!
2020-08-18
not 490000448.
[root@zheng /]# cat -n /zyf/jianli.txt 
     1  I am student!
     2  I like Liunx.
     3
     4  I like basketball
     5  I like badminton ball ,billiard ball and chinese chess!
     6  my blog is http://oldboy.blog.51cto.com
     7  our size is http://blog.oldoyedu.com
     8  2020-08-18
     9  my WeChat is 13522828457
    10
    11  not 490000448.
    12  my god ,i an not oldbey,but OlDEBOY!
    13  ....******!!!!!!!|||||||#####^^^^^
    14  2020-08-20

搜索文件信息

搜索一种信息

-n 取消默认输出
搜索有blog的行
搜索字符要用两个 / /包起来

/要搜索的内容/p

[root@zheng ~]# sed -n '/blog/p' /zyf/jianli.txt 
my blog is http://oldboy.blog.51cto.com
our size is http://blog.oldoyedu.com

搜索一种以上的信息

-r 在脚本中使用扩展正则表达式,要不然后面的 | 参数会报错
搜索blog和135所在的行
中间的丨是分隔开,什么和什么的意思

[root@zheng ~]# sed -nr '/blog|135/p' /zyf/jianli.txt  
my blog is http://oldboy.blog.51cto.com
our size is http://blog.oldoyedu.com
my WeChat is 13522828457

搜索从这个字符到另一个字符之间的所有行

nl +文件名显示行号
搜索从blog到not之间的行
用于查日志,搜索从多少时间到多少时间

 '/搜索的第一个字符/,/搜索的第二个字符/p'
[root@zheng ~]# nl /zyf/jianli.txt |sed -n '/blog/,/not/p' 
     5  my blog is http://oldboy.blog.51cto.com
     6  our size is http://blog.oldoyedu.com
     7  my WeChat is 13522828457
       
     8  not 490000448.

日志搜索日期到日期

先添加两行日期
在第8行和最后一行添加两个日期

[root@zheng /]# sed -i '7a\2020-08-18\' /zyf/jianli.txt  
[root@zheng /]# sed -i '$a\2020-08-20\' /zyf/jianli.txt

查看两个日期中间的信息

[root@zheng /]# sed -nr '/2020-08-18/,/2020-08-20/p' /zyf/jianli.txt 
2020-08-18
my WeChat is 13522828457

not 490000448.
my god ,i an not oldbey,but OlDEBOY!
....******!!!!!!!||

查看文件内的奇偶数行

[root@zheng /]# seq 10 |sed -n '1~2p' 
1
3
5
7
9
[root@zheng /]# seq 10 |sed -n '0~2p'     
2
4
6
8
10

搜索从这个字符的所在行到最后一行信息

nl显示行号
$是最后一行,不是正则表达式,所以不用加 / /
但是前面的的字符需要加 / /
从WeChat到到最后一行

[root@zheng ~]# nl /zyf/jianli.txt |sed -n '/WeChat/,$p'  
     7  my WeChat is 13522828457
       
     8  not 490000448.
     9  my god ,i an not oldbey,but OlDEBOY!
    10  ....******!!!!!!!|||||||#####^^^^^

在显示内容的时候加上别的信息

在第7行后加上looper,显示在第8行
注意是显示添加,并不实际加入到文件中,只显示

[root@shell ~ 6]# sed -e 7a\looper sed.txt
uzi!!!
xiaohu
mlxg
letme
ming
karsa
heart
looper
mata
Zz1tai
gogogo!!!

替换内容

‘s#需要被替换的#替换的#g’

让他显示到屏幕上是被替换的
注意现在只是显示出来是替换的,并没有改文件的内容

[root@shell ~ 85]# cat >/root/sed.txt <<EOF 
uzi
xiaohu
mlxg
letme
ming
karsa
heart
mata
Zz1tai
dddd

> 
> EOF
[root@shell ~ 86]# cat sed.txt 
uzi
xiaohu
mlxg
letme
ming
karsa
heart
mata
Zz1tai
dddd


[root@shell ~ 87]# sed 's#dddd#gogogo!!!#g' /root/sed.txt 
uzi
xiaohu
mlxg
letme
ming
karsa
heart
mata
Zz1tai
gogogo!!!

要是修改文件内容的话需要加上 -i 参数
-i 修改文本内容
一般不直接使用-i,先用替换查询一下,查看没出错以后在加上-i参数


[root@shell ~ 90]# sed -i 's#dddd#gogogo!!!#g' /root/sed.txt   
[root@shell ~ 91]# cat sed.txt 
uzi
xiaohu
mlxg
letme
ming
karsa
heart
mata
Zz1tai
gogogo!!!

##  替换文件的时候备份
-i  后面加扩展名可以直接加到这个源文件的后面
一个该之前的一个改之后的
```c

[root@shell ~ 3]# sed -i.bak 's#uzi#uzi!!!#g' sed.txt 
[root@shell ~ 4]# cat sed.txt
uzi!!!
xiaohu
mlxg
letme
ming
karsa
heart
mata
Zz1tai
gogogo!!!


[root@shell ~ 5]# cat sed.txt
sed.txt      sed.txt.bak  
[root@shell ~ 5]# cat sed.txt.bak 
uzi
xiaohu
mlxg
letme
ming
karsa
heart
mata
Zz1tai
gogogo!!!

将第5-10行的内容取代成为gogog!!!

这个也只是列出而已,并没有写入文件,如果要写入可以直接在sed后面加上-i 参数

[root@shell ~ 56]# nl sed.txt
     1  uzi!!!
     2  xiaohu
     3  mlxg
     4  letme
     5  mata> able
     6  mata > able
     7  mata able
     8  matable
     9  ming
    10  ming
[root@shell ~ 57]# nl sed.txt |sed '5,10c gogogo!!!'
     1  uzi!!!
     2  xiaohu
     3  mlxg
     4  letme
gogogo!!!

查询ip地址并过滤掉其他内容

先查询 ens33网卡信息
/sbin/ifconfig和ifconfig是一样的

[root@zheng ~]# /sbin/ifconfig ens33 
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.100.10  netmask 255.255.255.0  broadcast 192.168.100.255
        inet6 fe80::299c:3370:1a37:a22d  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:0c:2b:06  txqueuelen 1000  (Ethernet)
        RX packets 1841265  bytes 2078480780 (1.9 GiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 372021  bytes 174680960 (166.5 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

只匹配inet内容的行,inet后面要加一个空格要不然inet6一起显示出来

[root@zheng ~]# ifconfig ens33 |grep 'inet ' 
        inet 192.168.100.10  netmask 255.255.255.0  broadcast 192.168.100.255

把ip开头的inet去掉,两种方法

1
[root@zheng ~]# ifconfig ens33 |grep 'inet ' |sed 's/^.*inet//g'
 192.168.100.10  netmask 255.255.255.0  broadcast 192.168.100.255
2
[root@zheng ~]# ifconfig ens33 |grep 'inet ' |sed 's/inet/ /g'
          192.168.100.10  netmask 255.255.255.0  broadcast 192.168.100.255

只保留ip地址,后面的内容全部去掉

[root@zheng ~]# ifconfig ens33 |grep 'inet ' |sed 's/inet/ /g'|sed 's/netmask.*$//g'
          192.168.100.10  

用替换来删除

随便找个文件
s == substitute sub 替换
g == global 全局,会默认替换第一个匹配的字符
把所有数字都替换成空 ‘s#[0-9]##g’
把第一列数字替换成空 ‘s#[0-9]##’

[root@zheng ~]# cat test.txt 
金 211324198415244720500224197105168312646461asd
任 123123123zyf
任 linux6543454345354
任 j516515615635433
任 50182197306dagasdd
吕 21128219920911303X
空 15000001932111261125513321513152513119264611813621162X
杜 11010151351332153x
[root@zheng ~]# sed 's#[0-9]##g' test.txt 
金 
万 
万 asd
任 zyf
任 linux
任 j
任 dagasdd
吕 X
空 
控 
向 X
杜 x

[root@zheng ~]# sed 's#[0-9]##' test.txt  
金 113241984152447200022419710516831246461asd
任 23123123zyf
任 linux543454345354
任 j16515615635433
任 0182197306dagasdd
吕 1128219920911303X
空 50000019321112611551332151315251319264611813621162X
杜 1010151351332153x

添加内容

添加一行内容

[root@shell ~ 9]# sed -i 7a\looper sed.txt 
[root@shell ~ 10]# cat sed.txt
uzi!!!
xiaohu
mlxg
letme
ming
karsa
heart
looper
mata
Zz1tai
gogogo!!!

添加多行内容

在最后一行添加test111和test22

[root@zheng ~]# nl sed.txt |sed  '$a\test111\
> test22'
     1  uzi!!!
     2  xiaohu
     3  mlxg
     4  letme
     5  ming
     6  karsa
     7  heart
     8  looper
     9  mata
    10  Zz1tai
    11  gogogo!!!
test111
test22
[root@zheng ~]# nl sed.txt |sed -i '$a\test111\
test22'
sed: no input files   #这里提示没有文件显示,加上那个文件内容名就好了
[root@zheng ~]# nl sed.txt |sed -i '$a\test111\
test22' sed.txt 
[root@zheng ~]# nl sed.txt 
     1  uzi!!!
     2  xiaohu
     3  mlxg
     4  letme
     5  ming
     6  karsa
     7  heart
     8  looper
     9  mata
    10  Zz1tai
    11  gogogo!!!
    12  test111
    13  test22

删除内容并写入文件

将内容列出并且列印行号然后删除11行

#显示行号
[root@shell ~ 25]# nl sed.txt
     1  uzi!!!
     2  xiaohu
     3  mlxg
     4  letme
     5  ming
     6  karsa
     7  heart
     8  looper
     9  mata
    10  Zz1tai
    11  gogogo!!!
       
       
[root@shell ~ 30]# sed -i '8,10d' sed.txt 
[root@shell ~ 31]# nl sed.txt
     1  uzi!!!
     2  xiaohu
     3  mlxg
     4  letme
     5  ming
     6  karsa
     7  heart

从第5行删除到最后一行

[root@shell ~ 32]# sed -i '5,$d' sed.txt   
[root@shell ~ 33]# nl sed.txt
     1  uzi!!!
     2  xiaohu
     3  mlxg
     4  letme

写入内容

nl +file 显示行号,在第四行后,第五行加上ming

[root@shell ~ 45]# nl sed.txt|sed -i '4a ming' sed.txt  
[root@shell ~ 48]# nl sed.txt
     1  uzi!!!
     2  xiaohu
     3  mlxg
     4  letme
     5  ming

添加空行

[root@zheng ~]# nl sed.txt |sed -i '1a  \\' sed.txt 
[root@zheng ~]# nl sed.txt 
     1  uzi!!!
       
     2  AAABBBCCC
     3  AAABBBCCC
     4  xiaohu
     5  mlxg
     6  letme
     7  ming
     8  karsa
     9  heart
    10  looper
    11  mata
    12  Zz1tai
    13  gogogo!!!
    14  test111
    15  test22
[root@zheng ~]# nl sed.txt |sed -i '1a  \\n' sed.txt 
[root@zheng ~]# nl sed.txt 
     1  uzi!!!
       
       
       
     2  AAABBBCCC
     3  AAABBBCCC
     4  xiaohu
     5  mlxg
     6  letme
     7  ming
     8  karsa
     9  heart
    10  looper
    11  mata
    12  Zz1tai
    13  gogogo!!!
    14  test111
    15  test22

数据的搜寻并显示

搜寻带有ming关键字的行
搜索匹配的行会和之前的一起显示出来
sed 后面 单引号括起来
搜索关键词用两个斜杠包起来最后加上 p

[root@shell ~ 7]# nl sed.txt |sed '/ma/p'  
     1  uzi!!!
     2  xiaohu
     3  mlxg
     4  letme
     5  mata> able
     5  mata> able
     6  mata > able
     6  mata > able
     7  mata able
     7  mata able
     8  matable
     8  matable
     9  ming
    10  ming

这种方法看着很不清晰
所以要加一个-n 取消静默输出,让他只打印输出匹配行

[root@shell ~ 9]# nl sed.txt |sed -n '/mata/p'
     5  mata> able
     6  mata > able
     7  mata able
     8  matable

数据的搜寻并执行命令

搜索/etc/passwd,找到root对应的行,执行后面花括号中的一组命令,每个命令之间用分号分隔,这里把bash替换为blueshell,再输出这行:

p :打印,亦即将某个选择的数据印出。通常 p 会与参数 sed -n 一起运行~
s :取代,可以直接进行取代的工作哩!通常这个 s 的动作可以搭配正规表示法!例如 1,20s/old/new/g
q : 是退出。

     8  matable
[root@shell ~ 10]# cp /etc/passwd /root/passwd
[root@shell ~ 11]# nl sed.txt |sed -n '/root/{s/bash/blueshell/;p;q}'
[root@shell ~ 12]# nl /etc/passwd |sed -n '/root/{s/bash/blueshell/;p;q}'       
     1  root:x:0:0:root:/root:/bin/blueshell
[root@shell ~ 13]# nl /etc/passwd
     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
.......
.......
........
......

多点编辑

删除/etc/passwd第三行到末尾的数据,并把bash替换为blueshell
也是两种思路

1
[root@zheng ~]# nl /etc/passwd |sed '3,$d' |sed 's/bash/blueshell/g'
     1  root:x:0:0:root:/root:/bin/blueshell
     2  bin:x:1:1:bin:/bin:/sbin/nologin
2
[root@zheng ~]# nl /etc/passwd |sed -e '3,$d' -e 's/bash/blueshell/g'
     1  root:x:0:0:root:/root:/bin/blueshell
     2  bin:x:1:1:bin:/bin:/sbin/nologin

复杂操作

把每一行结尾为.的内容换成!号
.$当前行的最后

[root@zheng ~]# sed -i 's/\.$/\!/g' sed.txt 
[root@zheng ~]# nl sed.txt 
     1  linux!
     2  centos-
     3  google!
     4  taobao!
     5  facebook!
     6  zhihu-
     7  weibo-

基础总结

	d	#删除行
		sed '1,3d' test.xx	#删除13行
	a	#新增行
		sed '1a hello' test.txt	#第1行后新增hello
	i	#插入行
		sed '1i hello' test.txt	#在第1行插入hello
	c	#替换行
		sed '1c hello' test.txt	#替换匹配行为hello
		sed '/^SELINUX=/cSELINUX=disabled' test.txt
	p	#输出行(-n选项配合)
		sed -n '2p' test.txt	#输出第2行
	s	#替换匹配字符串
		sed 's/aa/AA/' test.txt				#替换每一行遇到的第一个aa为AA
		sed '2s/2/x/' test.txt				#替换第2行的第一个2为x
		sed '/^[0-9]/s/aa/AA/g' test.txt	#替换以数字开头的行的所有aa为AA
	g	#一行里所有都匹配
		sed 's/aa/AA/g' test.txt	#替换一行里所有的aa为AA

a、i和c的使用

参数意思
aappend 追加
iinsert 插入
cchange 替换

a

一行下追加
[root@zheng /]# sed  '1a11111111,test,!!!' /zyf/jianli.txt  |cat -n
     1  I am student!
     2  11111111,test,!!!
     3  I like Liunx.
     4
     5  I like basketball
     6  I like badminton ball ,billiard ball and chinese chess!
     7  my blog is http://oldboy.blog.51cto.com
     8  our size is http://blog.oldoyedu.com
     9  2020-08-18
    10  my WeChat is 13522828457
    11
    12  not 490000448.
    13  my god ,i an not oldbey,but OlDEBOY!
    14  ....******!!!!!!!|||||||#####^^^^^
    15  2020-08-20
| 多行追加

需要添加 -r 参数,正则表达式

[root@zheng /]# sed -r '/2020-08-18|2020-08-20/a!!!ABCDEFG!!!' /zyf/jianli.txt  |cat -n 
     1  I am student!
     2  I like Liunx.
     3
     4  I like basketball
     5  I like badminton ball ,billiard ball and chinese chess!
     6  my blog is http://oldboy.blog.51cto.com
     7  our size is http://blog.oldoyedu.com
     8  2020-08-18
     9  !!!ABCDEFG!!!
    10  my WeChat is 13522828457
    11
    12  not 490000448.
    13  my god ,i an not oldbey,but OlDEBOY!
    14  ....******!!!!!!!|||||||#####^^^^^
    15  2020-08-20
    16  !!!ABCDEFG!!!
\n 追加两行内容

在这两个日期下添家信息 /a
追加第二行,\n,\n代表回车,换行

[root@zheng /]# sed -r '/2020-08-18|2020-08-20/a!!!ABCDEFG!!!\n123123' /zyf/jianli.txt  |cat -n 
     1  I am student!
     2  I like Liunx.
     3
     4  I like basketball
     5  I like badminton ball ,billiard ball and chinese chess!
     6  my blog is http://oldboy.blog.51cto.com
     7  our size is http://blog.oldoyedu.com
     8  2020-08-18
     9  !!!ABCDEFG!!!
    10  123123
    11  my WeChat is 13522828457
    12
    13  not 490000448.
    14  my god ,i an not oldbey,but OlDEBOY!
    15  ....******!!!!!!!|||||||#####^^^^^
    16  2020-08-20
    17  !!!ABCDEFG!!!
    18  123123

i

[root@zheng /]# sed  '1i11111111,test,!!!' /zyf/jianli.txt  |cat -n 
     1  11111111,test,!!!
     2  I am student!
     3  I like Liunx.
     4
     5  I like basketball
     6  I like badminton ball ,billiard ball and chinese chess!
     7  my blog is http://oldboy.blog.51cto.com
     8  our size is http://blog.oldoyedu.com
     9  2020-08-18
    10  my WeChat is 13522828457
    11
    12  not 490000448.
    13  my god ,i an not oldbey,but OlDEBOY!
    14  ....******!!!!!!!|||||||#####^^^^^
    15  2020-08-20

c

[root@zheng /]# sed '13c   sed -c;' /zyf/jianli.txt  |cat -n
     1  I am student!
     2  I like Liunx.
     3
     4  I like basketball
     5  I like badminton ball ,billiard ball and chinese chess!
     6  my blog is http://oldboy.blog.51cto.com
     7  our size is http://blog.oldoyedu.com
     8  2020-08-18
     9  my WeChat is 13522828457
    10
    11  not 490000448.
    12  my god ,i an not oldbey,but OlDEBOY!
    13  sed -c;
    14  2020-08-20

过滤空格、连续空格、空行

1、空行
2、a空格回车
3、a回车
4、空格回车
5、空格s回车
6、空格s空格回车
7、空格s回车
cat -A 查看空格行空行

[root@zheng ~]# vi kong

a 
a
  
  s
 s 
 s
[root@zheng ~]# cat -An kong 
     1  $
     2  a $
     3  a$
     4    $
     5    s$
     6   s $
     7   s$

过滤纯空行

原本第一行的空没了

[root@zheng ~]# egrep -v '^$' kong |cat -An
     1  a $
     2  a$
     3    $
     4    s$
     5   s $
     6   s$

去除所有带有空格的行

只剩下换行和a换行

[root@zheng ~]# egrep -v ' ' kong |cat -An     
     1  $
     2  a$

去掉开头空格加回车行

少了第四行

[root@zheng ~]# egrep -v '^ +$' kong |cat -An
     1  $
     2  a $
     3  a$
     4    s$
     5   s $
     6   s$

去掉空行或者带开头有空格加回车的行

egrep

少了第一行和第四行

[root@zheng ~]# egrep -v '^$|^ +$' kong |cat -An
     1  a $
     2  a$
     3    s$
     4   s $
     5   s$

sed -r

少了第一行和第四行

[root@zheng ~]# sed -r '/^$|^ +$/d' kong |cat -An 
     1  a $
     2  a$
     3    s$
     4   s $
     5   s$

^ *$ 高级版本

*表示零次或零次以上
^ *$
*0 次 ^ *$ == ^$
*0次以上 ^ *$ == ^ +$

[root@zheng ~]# egrep -v  '^ *$' kong  |cat -An
     1  a $
     2  a$
     3    s$
     4   s $
     5   s$
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值