02--Linux的sed使用

  • sed是流编辑器,每一次读取一行到内存中,即称之为模式空间(pattern space)
  • 默认不修改原文件,如果需要修改需加-i参数
  • sed有模式空间及保持空间(hold sapce),默认打印模式空间中的内容到标准输出
  • sed读取每行的时候会将内容保存至内存中
  • 支持正则和扩展正则表达式,除-y选项

  • 命令行格式:将包含sed的命令写在命令行中执行
  • $ sed [options] 'command' files

[Options]:

  • -n : 与p(print)命令合用时,表示只显示被选中的行,而不是显示所有的行,然后被选中的行会显示两次。
  • -e : 可以指定多个command
  • -f FILE : FILE中每行是一个操作命令
  • -r : 支持扩展正则表达式
  • -i : 与p(print)命令合用时,表示只显示被选中的行,而不是显示所有的行,然后被选中的行会显示两次。 

行定位:

  • n;    选中1行,n表示行号
  • /pattern/  使用正则表达式,注意要包含在/..../之间 

 

[root@localhost test]# #打印第5行
[root@localhost test]# sed -n "5 p" data.txt
Maecheal    男  30  Washington      America
 
[root@localhost test]# #打印匹配"China"的记录
[root@localhost test]# sed -n "/china/ p" data.txt
[root@localhost test]# sed -n "/China/ p" data.txt
小红        女  20  BeiJing         China
小明        男  22  ChongQing       China
花花        女  30  HeBei           China

选择多行,同样有两种方式:

  • n;    选中1行,n表示行号
  • /pattern/  使用正则表达式,注意要包含在/..../之间
[root@localhost test]# #打印第5行
[root@localhost test]# sed -n "5 p" data.txt
Maecheal    男  30  Washington      America
 
[root@localhost test]# #打印匹配"China"的记录
[root@localhost test]# sed -n "/china/ p" data.txt
[root@localhost test]# sed -n "/China/ p" data.txt
小红        女  20  BeiJing         China
小明        男  22  ChongQing       China
花花        女  30  HeBei           China

 


选择多行,行与行之间

  • x,y;    选中行号在x~y之间的行
  • /pattern1/, /pattern2/    选择匹配两个正则表达式之间的行
[root@localhost test]# #打印第3~6行
[root@localhost test]# cat -n data.txt | sed -n "3,6 p"
     3  花花        女  30  HeBei           China
     4  Jane        女  29  Los Angeles     America
     5  Maecheal    男  30  Washington      America
     6  山本        男  25  Nagasaki        Japan
 
[root@localhost test]# #打印“Jane”的行,到“贝爷”之间的行
[root@localhost test]# sed -n "/Jane/, /贝爷/ p" data.txt
Jane        女  29  Los Angeles     America
Maecheal    男  30  Washington      America
山本        男  25  Nagasaki        Japan
村上春树    男  40  Hiroshima       Japan
贝爷        男  35  Paris           Franch

不选择某一行或者某几行

  • 在后面加 ! 即可
[root@localhost test]# #打印除了第4行以外的所有行
[root@localhost test]# cat -n data.txt | sed -n "4! p"
     1  小红        女  20  BeiJing         China
     2  小明        男  22  ChongQing       China
     3  花花        女  30  HeBei           China
     5  Maecheal    男  30  Washington      America
     6  山本        男  25  Nagasaki        Japan
     7  村上春树    男  40  Hiroshima       Japan
     8  贝爷        男  35  Paris           Franch
 
[root@localhost test]# #打印除3-7行之外的行
[root@localhost test]# cat -n data.txt | sed -n "3,7! p"
     1 小红        女  20  BeiJing         China
     2  小明        男  22  ChongQing       China
     8  贝爷        男  35  Paris           Franch

间隔几行选择

  • 使用x~y格式,首先打印第x行,然后每个y行,就打印一次
[root@localhost test]# #打印第3行,之后,每隔2行,就打印一次
[root@localhost test]# cat -n data.txt | sed -n "3~2 p"
     3  花花        女  30  HeBei           China
     5  Maecheal    男  30  Washington      America
     7  村上春树    男  40  Hiroshima       Japan

sed有几个基本的操作命令,分别是下面几个:

  • -a (append,添加,在行后追加)
  • -i(insert,插入,在行前插入)
  • -d(delete,删除行)
  • -c(chage,替换)
  • -s(subsitute,替换)

-a 增加行

[root@localhost test]# #在第3行后面增加一行“---------------”
[root@localhost test]# cat -n data.txt | sed "3a -------------"
     1  小红        女  20  BeiJing         China
     2  小明        男  22  ChongQing       China
     3  花花        女  30  HeBei           China
-------------
     4  Jane        女  29  Los Angeles     America
     5  Maecheal    男  30  Washington      America
     6  山本        男  25  Nagasaki        Japan
     7  村上春树    男  40  Hiroshima       Japan
     8  贝爷        男  35  Paris           Franch
 
      
[root@localhost test]# #在3~5行的每一行后面加一行“==========”
[root@localhost test]# cat -n data.txt | sed "3,5a =========="
     1  小红        女  20  BeiJing         China
     2  小明        男  22  ChongQing       China
     3  花花        女  30  HeBei           China
==========
     4  Jane        女  29  Los Angeles     America
==========
     5  Maecheal    男  30  Washington      America
==========
     6  山本        男  25  Nagasaki        Japan
     7  村上春树    男  40  Hiroshima       Japan
     8  贝爷        男  35  Paris           Franch
 
 
[root@localhost test]# #在每一行后面增加一行“===========”
[root@localhost test]# cat -n data.txt | sed "a =========="
     1  小红        女  20  BeiJing         China
==========
     2  小明        男  22  ChongQing       China
==========
     3  花花        女  30  HeBei           China
==========
     4  Jane        女  29  Los Angeles     America
==========
     5  Maecheal    男  30  Washington      America
==========
     6  山本        男  25  Nagasaki        Japan
==========
     7  村上春树    男  40  Hiroshima       Japan
==========
     8  贝爷        男  35  Paris           Franch
==========
 
[root@localhost test]# #在行末增加一行“-------------------”
[root@localhost test]# sed '$a -------------------------' data.txt
小红        女  20  BeiJing         China
小明        男  22  ChongQing       China
花花        女  30  HeBei           China
Jane        女  29  Los Angeles     America
Maecheal    男  30  Washington      America
山本        男  25  Nagasaki        Japan
村上春树    男  40  Hiroshima       Japan
贝爷        男  35  Paris           Franch

-i 插入行

  • -i插入行和增加行的操作一样,区别是a是在行之后增加,i是在行之前插入
[root@localhost test]# #在第3行之前增加一行“---------------”
[root@localhost test]# cat -n data.txt | sed "3i --------------"
     1  小红        女  20  BeiJing         China
     2  小明        男  22  ChongQing       China
--------------
     3  花花        女  30  HeBei           China
     4  Jane        女  29  Los Angeles     America
     5  Maecheal    男  30  Washington      America
     6  山本        男  25  Nagasaki        Japan
     7  村上春树    男  40  Hiroshima       Japan
     8  贝爷        男  35  Paris           Franch
 
[root@localhost test]# #在第3~5行的每一行之前插入一行"==========="
[root@localhost test]# cat -n data.txt | sed "3,5i ==========="
     1  小红        女  20  BeiJing         China
     2  小明        男  22  ChongQing       China
===========
     3  花花        女  30  HeBei           China
===========
     4  Jane        女  29  Los Angeles     America
===========
     5  Maecheal    男  30  Washington      America
     6  山本        男  25  Nagasaki        Japan
     7  村上春树    男  40  Hiroshima       Japan
     8  贝爷        男  35  Paris           Franch
[root@localhost test]# #在每一行之前插入一行"==========="
[root@localhost test]# cat -n data.txt | sed "i ==========="
===========
     1  小红        女  20  BeiJing         China
===========
     2  小明        男  22  ChongQing       China
===========
     3  花花        女  30  HeBei           China
===========
     4  Jane        女  29  Los Angeles     America
===========
     5  Maecheal    男  30  Washington      America
===========
     6  山本        男  25  Nagasaki        Japan
===========
     7  村上春树    男  40  Hiroshima       Japan
===========
     8  贝爷        男  35  Paris           Franch

-c 替换行

  • 替换行,是指,将指定行,整行内容都替换为指定内容,注意-s是指替换行中的一部分内容。
  • 注意,区间替换的时候,是整体替换,而不是逐行替换。
[root@localhost test]# #将第2行替换为"hello shell"
[root@localhost test]# cat -n data.txt | sed "2c hello world"
     1  小红        女  20  BeiJing         China
hello world
     3  花花        女  30  HeBei           China
     4  Jane        女  29  Los Angeles     America
     5  Maecheal    男  30  Washington      America
     6  山本        男  25  Nagasaki        Japan
     7  村上春树    男  40  Hiroshima       Japan
     8  贝爷        男  35  Paris           Franch
 
[root@localhost test]# #尝试将第2~5行的每一行都替换为"hello world",但是实际操作后会将2-5行整体替换
[root@localhost test]# cat -n data.txt | sed "2,5c hello world"
     1  小红        女  20  BeiJing         China
hello world
     6  山本        男  25  Nagasaki        Japan
     7  村上春树    男  40  Hiroshima       Japan
     8  贝爷        男  35  Paris           Franch
 
[root@localhost test]# #将每一行都替换为“hello world”
[root@localhost test]# cat -n data.txt | sed "c hello world"
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world

-d 删除行

[root@localhost test]# #删除第4行
[root@localhost test]# cat -n data.txt | sed "4d"
     1  小红        女  20  BeiJing         China
     2  小明        男  22  ChongQing       China
     3  花花        女  30  HeBei           China
     5  Maecheal    男  30  Washington      America
     6  山本        男  25  Nagasaki        Japan
     7  村上春树    男  40  Hiroshima       Japan
     8  贝爷        男  35  Paris           Franch
 
[root@localhost test]# #删除第4-6行
[root@localhost test]# cat -n data.txt | sed "4,6d"
     1  小红        女  20  BeiJing         China
     2  小明        男  22  ChongQing       China
     3  花花        女  30  HeBei           China
     7  村上春树    男  40  Hiroshima       Japan
     8  贝爷        男  35  Paris           Franch
 
[root@localhost test]# #删除所有行(无意义)
[root@localhost test]# cat -n data.txt | sed "d"

-s 替换行的部分内容

[root@localhost test]# #将字符a替换为X
[root@localhost test]# sed 's/a/X/' data.txt
小红        女  20  BeiJing         ChinX
小明        男  22  ChongQing       ChinX
花花        女  30  HeBei           ChinX
JXne        女  29  Los Angeles     America
MXecheal    男  30  Washington      America
山本        男  25  NXgasaki        Japan
村上春树    男  40  HiroshimX       Japan
贝爷        男  35  PXris           Franch
  • 注意,在替换的时候,只替换了一次,即只替换第一个匹配的内容。如果要将满足条件的内容都替换,就需要加上g
[root@localhost test]# sed 's/a/X/g' data.txt
小红        女  20  BeiJing         ChinX
小明        男  22  ChongQing       ChinX
花花        女  30  HeBei           ChinX
JXne        女  29  Los Angeles     AmericX
MXecheXl    男  30  WXshington      AmericX
山本        男  25  NXgXsXki        JXpXn
村上春树    男  40  HiroshimX       JXpXn
贝爷        男  35  PXris           FrXnch

sed使用案例

1、在data文件的第8行下面增加一行“hello world”,并且hello world前面要有4个空格

[root@localhost test]# #测试1,直接输入4个空格,失败
[root@localhost test]# sed '8 a     hello world' data.txt
小红        女  20  BeiJing         China
小明        男  22  ChongQing       China
花花        女  30  HeBei           China
Jane        女  29  Los Angeles     America
Maecheal    男  30  Washington      America
山本        男  25  Nagasaki        Japan
村上春树    男  40  Hiroshima       Japan
贝爷        男  35  Paris           Franch
hello world
 
[root@localhost test]# #测试2,使用反斜线转义,成功
[root@localhost test]# sed '8 a \    hello world' data.txt
小红        女  20  BeiJing         China
小明        男  22  ChongQing       China
花花        女  30  HeBei           China
Jane        女  29  Los Angeles     America
Maecheal    男  30  Washington      America
山本        男  25  Nagasaki        Japan
村上春树    男  40  Hiroshima       Japan
贝爷        男  35  Paris           Franch
    hello world

2、删除demo.txt文件中的空行

[root@localhost test]# cat demo.txt
111111
 
22222
 
333333
44444
[root@localhost test]# sed '/^$/ d' demo.txt
111111
22222
333333
44444

3、获取eth0网卡的ip

[root@localhost test]# ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 00:0C:29:21:0C:0F
          inet addr:192.168.228.153  Bcast:192.168.228.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe21:c0f/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:27644 errors:0 dropped:0 overruns:0 frame:0
          TX packets:14175 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:22947297 (21.8 MiB)  TX bytes:1135056 (1.0 MiB)
 
[root@localhost test]# ifconfig eth0 | sed -n '/inet addr:/ p'
          inet addr:192.168.228.153  Bcast:192.168.228.255  Mask:255.255.255.0
 
[root@localhost test]# ifconfig eth0 | sed -n '/inet addr:/ p' | sed 's/.*inet addr://'
192.168.228.153  Bcast:192.168.228.255  Mask:255.255.255.0
 
[root@localhost test]# ifconfig eth0 | sed -n '/inet addr:/ p' | sed 's/.*inet addr://' | sed 's/B.*$//'
192.168.228.153

高级sed操作

使用花括号{   }将多个sed命令包含在一起,多个sed之间用;分开

[root@localhost test]# cat -n data.txt
     1  小红        女  20  BeiJing         China
     2  小明        男  22  ChongQing       China
     3  花花        女  30  HeBei           China
     4  Jane        女  29  Los Angeles     America
     5  Maecheal    男  30  Washington      America
     6  山本        男  25  Nagasaki        Japan
     7  村上春树    男  40  Hiroshima       Japan
     8  贝爷        男  35  Paris           Franch
[root@localhost test]# cat -n data.txt | sed '{3,5 d; s/China/Chinese/}'
     1  小红        女  20  BeiJing         Chinese
     2  小明        男  22  ChongQing       Chinese
     6  山本        男  25  Nagasaki        Japan
     7  村上春树    男  40  Hiroshima       Japan
     8  贝爷        男  35  Paris           Franch

打印奇数行和偶数行

[root@localhost test]# #打印奇数行
[root@localhost test]# cat -n data.txt | sed -n '1~2 p'
     1  小红        女  20  BeiJing         China
     3  花花        女  30  HeBei           China
     5  Maecheal    男  30  Washington      America
     7  村上春树    男  40  Hiroshima       Japan
[root@localhost test]# cat -n data.txt | sed -n '{p; n}'
     1  小红        女  20  BeiJing         China
     3  花花        女  30  HeBei           China
     5  Maecheal    男  30  Washington      America
     7  村上春树    男  40  Hiroshima       Japan
[root@localhost test]#
[root@localhost test]# #打印偶数行
[root@localhost test]# cat -n data.txt | sed -n '2~2 p'
     2  小明        男  22  ChongQing       China
     4  Jane        女  29  Los Angeles     America
     6  山本        男  25  Nagasaki        Japan
     8  贝爷        男  35  Paris           Franch
[root@localhost test]# cat -n data.txt | sed -n '{n; p}'
     2  小明        男  22  ChongQing       China
     4  Jane        女  29  Los Angeles     America
     6  山本        男  25  Nagasaki        Japan
     8  贝爷        男  35  Paris           Franch

&表示前面已经匹配的字符串内容,反向引用,不用再写一次正则表达式

  • 在男或者女之前加一个gender单词
[root@localhost test]# cat -n data.txt | sed 's/[男|女]/gender:[男|女]/'
     1  小红        gender:[男|女]  20  BeiJing         China
     2  小明        gender:[男|女]  22  ChongQing       China
     3  花花        gender:[男|女]  30  HeBei           China
     4  Jane        gender:[男|女]  29  Los Angeles     America
     5  Maecheal    gender:[男|女]  30  Washington      America
     6  山本        gender:[男|女]  25  Nagasaki        Japan
     7  村上春树    gender:[男|女]  40  Hiroshima       Japan
     8  贝爷        gender:[男|女]  35  Paris           Franch
[root@localhost test]# cat -n data.txt | sed 's/[男|女]/gender:&/'
     1  小红        gender:女  20  BeiJing         China
     2  小明        gender:男  22  ChongQing       China
     3  花花        gender:女  30  HeBei           China
     4  Jane        gender:女  29  Los Angeles     America
     5  Maecheal    gender:男  30  Washington      America
     6  山本        gender:男  25  Nagasaki        Japan
     7  村上春树    gender:男  40  Hiroshima       Japan
     8  贝爷        gender:男  35  Paris           Franch

  • 案例1:将data.txt中的所有字母都变为大写
[root@localhost test]# cat -n data.txt
     1  小红        女  20  BeiJing         China
     2  小明        男  22  ChongQing       China
     3  花花        女  30  HeBei           China
     4  Jane        女  29  Los Angeles     America
     5  Maecheal    男  30  Washington      America
     6  山本        男  25  Nagasaki        Japan
     7  村上春树    男  40  Hiroshima       Japan
     8  贝爷        男  35  Paris           Franch
[root@localhost test]# cat -n data.txt | sed 's/[A-Z]/\l&/g'
     1  小红        女  20  beijing         china
     2  小明        男  22  chongqing       china
     3  花花        女  30  hebei           china
     4  jane        女  29  los angeles     america
     5  maecheal    男  30  washington      america
     6  山本        男  25  nagasaki        japan
     7  村上春树    男  40  hiroshima       japan
     8  贝爷        男  35  paris           franch
[root@localhost test]# cat -n data.txt | sed 's/[a-z]/\u&/g'
     1  小红        女  20  BEIJING         CHINA
     2  小明        男  22  CHONGQING       CHINA
     3  花花        女  30  HEBEI           CHINA
     4  JANE        女  29  LOS ANGELES     AMERICA
     5  MAECHEAL    男  30  WASHINGTON      AMERICA
     6  山本        男  25  NAGASAKI        JAPAN
     7  村上春树    男  40  HIROSHIMA       JAPAN
     8  贝爷        男  35  PARIS           FRANCH

-r 复制指定文件插入到匹配行

  • 将A.txt中的内容,插入到B.txt的第2行后面
[root@localhost test]# #将A.txt中的内容插入到B.txt中的第2行后面
[root@localhost test]# sed '2 r A.txt' B.txt
AAAAAAA
BBBBBBB
111111
222222
333333
CCCCCCC
[root@localhost test]# #将A.txt中的内容插入到B.txt中包含CCCCCC的行后面
[root@localhost test]# sed '/CCCCCC/ r A.txt' B.txt
AAAAAAA
BBBBBBB
CCCCCCC
111111
222222
333333
[root@localhost test]# #将A.txt中的内容插入B.txt中每一行的后面
[root@localhost test]# sed 'r A.txt' B.txt
AAAAAAA
111111
222222
333333
BBBBBBB
111111
222222
333333
CCCCCCC
111111
222222
333333

-w 复制匹配行,拷贝到指定文件中

  • 对于A.txt中选中的行,保存到文件B.txt中,会首先清空B.txt的内容,然后将选中的行拷贝出来,再保存到B.txt中。
[root@localhost test]# #将A.txt中的2-4行,写到C.txt中,注意会先清空C.txt
[root@localhost test]# sed '2,4 w C.txt' A.txt
111111
222222
333333
[root@localhost test]# cat C.txt
222222
333333

-q 提前退出sed

  • sed的处理流程是:从文件中读入一行,然后sed处理一行,一直到文件结束为止。
  • 使用q,可以让sed提前结束,不用读到文件结束。
[root@localhost test]# #打印前3行
[root@localhost test]# sed '3 q' data.txt
小红        女  20  BeiJing         China
小明        男  22  ChongQing       China
花花        女  30  HeBei           China
[root@localhost test]#
[root@localhost test]# #找到第1个Japan
[root@localhost test]# sed '/Japan/ q' data.txt
小红        女  20  BeiJing         China
小明        男  22  ChongQing       China
花花        女  30  HeBei           China
Jane        女  29  Los Angeles     America
Maecheal    男  30  Washington      America
山本        男  25  Nagasaki        Japan

\(  \)多次反向引用

  • 使用\1,\2,\3....\n表示前面的第n个子表达式
  • 获取所有用户,以及用户的UID和GID
[root@localhost test]# head -5 /etc/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
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
[root@localhost test]# head -5 /etc/passwd | sed 's/^\([a-zA-Z_-]\+\):x:\([0-9]\+\):\([0-9]\+\):.*/USER:\1 \t UID:\2  GID:\3/'
USER:root        UID:0  GID:0
USER:bin         UID:1  GID:1
USER:daemon      UID:2  GID:2
USER:adm         UID:3  GID:4
USER:lp          UID:4  GID:7

 

  • 获取eth0的ip地址
[root@localhost test]# ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 00:0C:29:21:0C:0F
          inet addr:192.168.228.153  Bcast:192.168.228.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe21:c0f/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:34864 errors:0 dropped:0 overruns:0 frame:0
          TX packets:17848 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:23505959 (22.4 MiB)  TX bytes:1547380 (1.4 MiB)
 
[root@localhost test]# ifconfig eth0 | sed -n '/inet addr/ p' | sed 's/.*inet addr:\([0-9.]\+\).*/\1/'
192.168.228.153

 


总结

指定单个文件查找

  • 方法一:cat test.log | grep “关键字”
  • 方法二:grep -i “关键字” ./test.log

查找指定时间段的日志

  • sed -n ‘/2020-01-01 13:10:10.294/, /2020-01-01 14:10:10.294/p’ test.log

关键字搜索指定时间段的日志

  • sed -n ‘/2020-01-01 13:10:10.294/, /2020-01-01 14:10:10.294/p’ test.log | grep “关键字”

如果想直接修改源文件,而没有这样的过程,可以用下面的命令

  • sed  -i 's/properties/property/g'  build.xml

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值