Sed of text processing tool

and

Line editor

sed是一种流编辑器,它一次处理一行内容。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。然后读入下行,执行下一个循环。
如果没有使诸如‘D’ 的特殊命令,那会在两个循环之间清空模式空间,但不会清空保留空间。这样不断重复,直到文件末尾。文件内容并没有改变,除非你使用重
定向存储输出。

Features

主要用来自动编辑一个或多个文件,简化对文件的反复操作,编写转换程序等

Common options

-n do not output the contents of the pattern space to the screen, that does not automatically print
-e multi-point editing
-f / PATH / SCRIPT_FILE edit the script to read from the specified file
-r supports extended regular expressions
-i.bak backup file and place edit

script 'Address command'

sed tool

Address delimitation

  • (1) not to address: the full text processing
  • (2) Single Address:
      #: specified line, $: last line
      / pattern /: here be able to match the pattern of each line
  • (3) the address range of
      #, #
      #, # +
      / PAT1 /, / PAT2 /
      #, / PAT1 /
  • (4) ~: step
    1 2 odd rows
    2 ~ sed tool even rows 2

Edit command

d space row deletion mode matching, and enable the next cycle immediately
p Print mode spatial content, then the default output is added to
a [] text line appends the specified text supports \ n multi-row additional
i [] text text is inserted before the row
c [] text replacement behavior single or multi-line text
w / path / file stored pattern matches the specified file to
the text-to-line space pattern matching to r / path / file read the specified file
= line of pattern space print line number
! space pattern matching row inversion processing tool sed
s /// Find Alternatively, other separators supports, s @@@, s ###

Replace mark

Alternatively the Global g line
p shows successful replacement row
w / PATH / FILE replaced successfully saved to a file line

Senior Editor command

P: Print pattern space beginning to \ n content, and before the default output is added to the
h: the contents of the pattern space cover to the holding space
H: the contents of the pattern space is added to the holding space
g: retrieve data from the holding space to cover the pattern space
G: remove the contents from the holding space is appended to the pattern space
x: the contents of the pattern space holding the contents of the space are interchangeable
n: read the next line to match the pattern space to cover
N: read taken to match the next line is appended to the pattern space
d: delete the pattern space line
D: if the pattern comprises a line feed space, the text mode until the first space in a newline removed, and does not read the new input line, and the pattern space using synthetic cycle restarts. If the pattern space does not contain line breaks, it will be like a normal issue d command to start a new cycle as

Exercise

\ 1. 3-6 to identify the row
Sed of text processing tool
\ 2. 3-3 identify rows + 2
Sed of text processing tool
\ 3. In line 2 added new line

Sed of text processing tool
\ 4. In line 11 added test line

Sed of text processing tool
\ 5. Matching saved in text at the beginning of the alias to alias.log in
Sed of text processing tool
\ 6. ⾏ increase ⼀ empty after each ⾏?

[root@centos ~]# sed 'G' text.txt

\ 7. Insert ⼀ empty ⾏ after matching the regex ⾏?

[root@centos ~]# sed '/regex/G' text.txt

\ 8. Sed will test files in the 50th ⾏ in haiwao changed haiwai?

[root@centos ~]# sed -n '50p' |sed 's/haiwao/haiwai/' test

\9. 替换一个文件/etc/passwd里的这root:x:0:0:root:/root:/bin/bash一行第二个root为test?

[root@centos ~]# cat /etc/passwd| sed '/^root/!d'|sed 's/root/test/2'

\10. 打印/etc/passwd的奇数⾏?

[root@centos ~]# sed -n '1~2p' /etc/passwd

\11. 打印出05点到12点之间的所有⽇志?打印出05:30:35到22:45:55之间的所有⽇志?

[root@centos ~]# sed -n '/2016\/06\/12 05:00:00/,/2016\/06\/12 12:00:00/p' a.log
[root@centos ~]# sed -n '/2016\/06\/12 05:30:35/,/2016\/06\/12 22:45:55/p' a.log

\12. 删除centos7系统/etc/grub2.cfg⽂件中所有以空⽩开头的⾏⾏⾸的空⽩字符

[root@centos ~]# sed -r 's/^[[:blank:]]+//' /etc/grub2.cfg

\13. 删除/etc/fstab⽂件中所有以#开头,后⾯⾄少跟⼀个空⽩字符的⾏的⾏⾸的# 和空⽩字符

[root@centos ~]# sed -r 's/^#[[:blank:]]+//'g /etc/fstab

\14. 在centos6系统/root/install.log每⼀⾏⾏⾸增加#号

[root@centos ~]# sed -r 's/.*/#&/' /root/install.log

\15. 在/etc/fstab⽂件中不以#开头的⾏的⾏⾸增加#号

[root@centos ~]# sed -r 's@^[^#]@#&@p' /etc/fstab

\16. 处理/etc/fstab路径,使⽤sed命令取出其⽬录名和基名
目录名:

[root@centos ~]# echo "/etc/fstab" |sed -r 's@^(./)([^/]+/?)$@\1@'
基名:
[root@centos ~]# echo "/etc/fstab" |sed -r 's@^(./)([^/]+/?)$@\2@'

\17. 利⽤sed 取出ifconfig命令中本机的IPv4地址

[root@centos ~]# ifconfig|sed -n '/broadcast/p'|sed -r 's@inet (.) netmask.@\1@'
[root@centos ~]# ifconfig ens33 |sed -r '2!d; s@(.inet )(.)(netmask.*)@\2@'

\18. 统计centos安装光盘中Package⽬录下的所有rpm⽂件的以.分隔倒数第⼆个字段的重复次数

[root@centos ~]# ls .rpm|sed -r 's@^..([^.]+).rpm$@\1@'|sort|uniq -c
[root@centos ~]# ls *.rpm|rev|cut -d. -f2|rev|sort|uniq -c

\ 19. /Etc/init.d/functions statistics the number of occurrences of each word in the file, and sort (Using grep and sed two kinds Remedies were achieved)

[root@centos ~]# sed -r "s/[^[:alpha:]]/\n/g" /etc/init.d/functions| sort | uniq -c | sort -nr
[root@centos ~]# egrep -o "[[:alpha:]]+" /etc/init.d/functions| sort | uniq -c | sort -nr

\ 20. The files Text is n and n + 1 is combined ⾏ ⼀ ⾏, n is an odd number ⾏

[root@centos ~]# seq 10 | sed "1~2N;s/\n/ /"

Guess you like

Origin blog.51cto.com/14234291/2443741