sed brief and model space

and

First, the function

1.sed

Stream EDitor, stream editor for editing text, ed

ed editor is ancient editor, difficult to use, do not believe themselves to try,

ed basic commands:

a - 在文件的末尾添加新内容
i - 在文件的最后一行之前插入新内容
c - 把文件输入的最后一行(不论是原来存在的或者插入的)替换成新内容
. - 退出编辑文件模式进入命令行模式(注意:此时并没有退出 **ed** 编辑器)
w - 保存文件
q - 真正退出 ed编辑器

Recommended death, give up.

2. Grammar

sed [option] ‘SCRIPTS’ FILE…

3. Options

  • -f FILE call sed script processing file

  • -n inhibition default content output, the command commonly used in conjunction p

  • -r support the expansion of metacharacters

  • -iSUFFIX modify the source file, and create a backup file: Source file name SUFFIX

  • -i use

[root@localhost tmp]#cat 1
a
b
c
d
e
f
g
[root@localhost tmp]#sed -i.bak '1,7d' 1
[root@localhost tmp]#cat 1.bak 
a
b
c
d
e
f
g
[root@localhost tmp]#cat 1

4.SCRIPT

  • [Address] [!] Command! Behind the command is not executed

  • Address {[Address] COMMAND; ...} for an address with a plurality of operation scripts

  • Address

    • Address space match all lines

    • $ The last line

    • address

      • Including a row address
  • N ~ M N rows from the beginning, every row M-1

    • N
  • N, MN to M rows

    • / PATTERN / regular matching lines
  • / PATTERN1 /, / PATTERN2 /
    Examples: Sed '/ A /, / B / D' Test

  • line-address

    • N N-th row
    • / PATTERN / regular matching lines

The last line of the contents of the output and the number of rows

[root@localhost tmp]#sed -n '$p' 1
g
[root@localhost tmp]#sed -n '$=' 1 
7

Output lines 2-3

[root@localhost tmp]#sed -n '2,3p' 1  
b
c

N ~ M Usage

Starting from the second row, every other line output

[root@localhost tmp]#sed -n '2~2p' 1  
b
d
f

B matches the beginning of the line

[root@localhost tmp]#sed -n '/^b/p' 1
b

B matches the beginning of the line to the beginning of the f

[root@localhost tmp]#sed -n '/^b/,/^f/p' 1
b
c
d
e
f
  • Command

    • Space Command Mode

      • d delete Delete the contents of the pattern space
  • p print displays the contents of the pattern space

    • s/PATTERN/REPLACE/g

For example

[root@localhost tmp]#sed -r 's/([^0-9]*):([0-9]):([^0-9]*)/\3\1\2/' /etc/inittab

S /// substitution command

[root@localhost tmp]#sed 's/a/b/g' 1
b
b
c
d
e
f
g

/ Can be replaced with other symbols s # / etc # / dev #

  • N

  • D

  • P

  • Holding space command

    • H/h
    • G / g
    • x

5.SED workflow

  • Read the new line to the pattern space, the "Scripts" first address match if they meet the command is executed
  • If the address is in line with the implementation of command, does not comply with an address command to remove
  • Until all addresses corresponding to the run command, the content output mode space

Second, the model space

1. Basic command

  • D to delete the contents of the pattern space
  • p print the contents of the pattern space
  • s /// FLAG search and replace
    FLAG g global match
  • a \ STRING append, additional content
  • i \ STRING insert, insert content
  • c \ STRING change, replace
  • = Print line number
  • l print control characters
  • character conversion y
    y / abc / ABC /
    a A a replacement
    to replace B b
    substitute c C
  • n next, next row is read to the pattern space
  • r FILE read the contents of the read file to the specified line
  • w FILE write lines to save the specified file
  • [Line-address] q quit exit
[root@localhost tmp]#sed -r '10q' filename
[root@localhost tmp]#sed -n 'l' 1
a$
b$
c$
d$
e$
f$
g$
[root@localhost tmp]#sed -n '=' 1 
1
2
3
4
5
6
7
[root@localhost tmp]#echo "abcdef" | sed 'y/abc/ABC/'
ABCdef

2. Advanced command

  • N reads the next line to the contents of the pattern space to \ n spliced ​​on a single line
  • P single line on the printing pattern space (\ n foregoing summary)
  • D delete the contents of a row on the pattern space, and continues to execute the command
    example
[root@localhost tmp]#sed '/^$/{N;/\n$/D}' test1111

Here Insert Picture Description

3. Comparison p; n, n; p, P; N, N; P

  • But p, n
[root@localhost tmp]#sed 'p;n' 1
a
a
b
c
c
d
e
e
f
g
g
  • n and p
[root@localhost tmp]#sed 'n;p' 1
a
b
b
c
d
d
e
f
f
g
  • P and N
[root@localhost tmp]#sed 'P;N' 1
a
a
b
c
c
d
e
e
f
g
g
  • But the N: P
[root@localhost tmp]#sed 'N;P' 1   
a
a
b
c
c
d
e
e
f
g

to sum up:

[root@localhost tmp]#sed -n 'p;n' 1                     
奇数行
a
c
e
g
[root@localhost tmp]#sed -n 'n;p' 1   
偶数行
b
d
f
[root@localhost tmp]#sed -n 'P;N' 1                     
a
c
e
g
[root@localhost tmp]#sed -n 'N;P' 1   
到奇数行时,不输出
a
c
e
Published 134 original articles · won praise 16 · views 6305

Guess you like

Origin blog.csdn.net/weixin_46108954/article/details/104763121
sed
Recommended