sed skills record

      sed is a flow the encoders, which is in very text processing tool that perfect fit it with a regular expression, function thereof can be extraordinary.

Handling, storing the row currently being processed in a temporary buffer, called a "model space" (oattern space), then the processing contents of the buffer to see with sed command, into the post-processing, the contents of the buffer sent to the screen display.

Then the next line processing, so repeated until the end of the file. Change the content of the document did not change unless the command written content updates.

Addressing is used to determine which rows to edit. The address may be a digital form, a regular expression, or a combination of both. If the address is not specified, sed will process all the input file lines.

sed command function

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

命令  功能

a\  在当前行后添加一行或多行。多行时除最后一行外,每行末尾需用“\”续行

 

c\  用此符号后的新文本替换当前行中的文本。多行时除最后一行外,每行末尾需用"\"续行

 

i\  在当前行之前插入文本。多行时除最后一行外,每行末尾需用"\"续行

 

d   删除行

 

h   把模式空间里的内容复制到暂存缓冲区

 

H   把模式空间里的内容追加到暂存缓冲区

 

g   把暂存缓冲区里的内容复制到模式空间,覆盖原有的内容

 

G   把暂存缓冲区的内容追加到模式空间里,追加在原有内容的后面

 

l   列出非打印字符

 

p   打印行

 

n   读入下一输入行,并从下一条命令而不是第一条命令开始对其的处理

 

q   结束或退出sed

 

r   从文件中读取输入行

 

!   对所选行以外的所有行应用命令

 

s   用一个字符串替换另一个

 

g   在行内进行全局替换

 

w   将所选的行写入文件

 

x   交换暂存缓冲区与模式空间的内容

 

y   将字符替换为另一字符(不能对正则表达式使用y命令)

sed common command parameters

1

2

3

4

5

6

7

p==print

d:delete

=:打印匹配行的行号

-n 取消默认的完整输出,只要需要的 

-e 允许多项编辑

-i 修改文件内容

-r 不需要转义

1

注意:& 符号在sed命令中代表上次匹配的结果

Frequently used commands show

sed good output line, with the source file:

1

2

3

4

5

6

7

8

9

10

11

12

hhh     pts/1        192.168.25.1     Sat Jun 30 22:04   still logged in  

reboot   system boot  2.6.32-358.el6.i Sat Jun 30 22:04 - 22:43  (00:38)   

omc     pts/0        192.168.25.1     Sat Jun 30 20:16 - down   (00:00)   

reboot   system boot  2.6.32-358.el6.i Sat Jun 30 19:38 - 20:16  (00:37)   

root     pts/1        192.168.25.1     Sat Jun 30 12:20 - down   (00:55)   

root     pts/0        192.168.25.1     Sat Jun 30 11:53 - down   (01:22)   

reboot   system boot  2.6.32-358.el6.i Sat Jun 30 11:52 - 13:15  (01:23)   

root     pts/0        192.168.25.1     Sat Jun 30 05:40 - down   (02:51)   

reboot   system boot  2.6.32-358.el6.i Sat Jun 30 05:38 - 08:32  (02:54)   

root     pts/0        192.168.25.1     Fri Jun 29 21:01 - down   (06:21)   

 

wtmp begins Tue Jun  9 03:57:56 2015

Print only the third line

1

sed -n '3p' /var/log/yum.log

image

View files only line 3 to line 9

1

sed -n '3,9p' /var/log/yum.log

image

Filtering a specific character string, displaying the contents of the line n  

1

sed -n  '/root/p' yum.log

image

Display comprising "hhh" row that contains the line between "omc" line

1

sed -n '/hhh/,/omc/p' yum.log

image

Print lines 1-5 and line numbers

1

sed -n -e '1,5p' -e '='  yum.log

image

Only the line number of the matched string

1

 sed -n '/root/p' yum.log

image

Print and sign matching rows behind and in accordance with [the equivalent of a keyword query]

1

sed -n -e '/root/p' -e '/root/=' yum.log

image

Replace yum.log document root with the real world [written]

1

2

3

4

5

sed -i 's/root/world/g' yum.log

==>sed -i 's#hhh#HHHH#g' h.txt     与上同

              s==search  查找并替换

              g==global  全部替换

              -i: implace

image

Print the last line

1

sed -n '$p' yum.log

image

Add happy in the first line of the file, end of file add the new year

1

2

sed -e '1i happy' -e '$a new year' yum.log       【界面显示】

sed -i -e '1i happy' -e '$a new year' yum.log    【真实写入文件】

 

image

Hahaha added in each line of the file first and fourth rows below

1

sed '1,4i hahaha' yum.log

image

& Symbol represents the result of the last match in the sed command

1

sed 's/world/hello_&/g' yum.log

image

 

Delete the first 3 to line 9, just do not show it

1

sed  '3,9d' /var/log/yum.log

image

Delete contains "hhh" line to include the line between "omc" row

1

sed '/hhh/,/omc/d' yum.log

image

 

Delete the contents included "omc" line to the tenth row

1

sed '/omc/,10d' yum.log

image
NOTE: In figures can be used, a regular expression, or a combination of both represent

5 to 10 show matching line in the root, the root of all the rows replaced FTL, and printed to the screen

1

2

sed '5,10 s/root/FTL/g' yum.log【仅显示用】

sed -i sed '5,10 s/root/FTL/g' yum.log【-i 会真正替换信息】

 

image

 

-e is editing commands for multiple editing tasks

1

2

删除1到5行后,替换reboot为shutdown

sed -e '1,5d' -e '=' -e 's/reboot/shutdown/g'  yum.log 

image

Print lines 3-6, and replace the matching shutdown reboot

1

sed -n -e '3,6p' -e 's/reboot/shutdown/g'  yum.log

image

-r命令是读命令。sed使用该命令将一个文本文件中的内容加到当前文件的特定位置上

1

2

[root@localhost log]# cat test.txt

[root@localhost log]# sed '/root/ r /var/log/test.txt' yum.log  【yum.log  匹配root后读入text.txt的内容在匹配内容之下,仅显示用】

image

-w是写入命令

1

2

3

sed  '/root/ w /home/omc/root.txt' yum.log       【yum.log匹配root后写入root.txt',真实写入】

sed -n '/root/ w /home/omc/root.txt' yum.log    【添加-n参数后,不打印源文件】

ll /home/omc/root.txt

image

打印1-5行,并显示行号

1

sed -n -e '1,5p' -e '='  yum.log

image

sed的正则匹配

正则匹配IP和子网掩码

1

2

3

4

[root@localhost log]#  ifconfig |sed -n '2p'

      [root@localhost log]# ifconfig |sed -n '2p' | sed -r 's#.*r:(.*) B.*k:(.*)#\1 \2#g'

 

      (.*)表示匹配的项,之后可以用\1取出第一个括号内匹配的内容,\2取出第二个括号内匹配的内容 

 

image

配合find命令的使用

1

2

3

4

find . -name  "*.txt" |xargs   sed -i 's/hhhh/\hHHh/g'

find . -name  "*.txt" |xargs   sed -i 's#hhhh#hHHh#g'

find . -name  "*.txt" -exec sed -i 's/hhhh/\hHHh/g' {} \;

find . -name  "*.txt" |xargs cat

sed错误使用

1

2

sed 's#,# #h' h.txt | cut -d" " -f 3,5 h.txt    <-- 错误的用法,前面已经处理了,后面又用cut查看h.txt文件

sed 's#,# #h' h.txt | cut -d" " -f 3,5          --> 正确的用法

发布了84 篇原创文章 · 获赞 149 · 访问量 65万+

Guess you like

Origin blog.csdn.net/pfm685757/article/details/104747882
sed