Detailed usage and explanation of sed

#Syntax:
sed [options] 'command' in_file[s]

options section:
-n silent output (do not print default output)

-e The -e option is required when giving multiple commands to sed #sed
-e 's/root/haha/g' -e 's/bash/wwwww/g' passwd > passwd.bak

If you do not use the -e option, you can also use a semicolon ";" to separate multiple commands.
#sed 's/haha/ro/g ; s/wwwww/kkkk/g' passwd | less

If there is no extension after -i -i, modify the file directly. If there is an extension to backup the source file, a new file ending with the extension will be generated
#sed -iback1 -e 's/root/rottt/g' -e 's/bash /wwwww/g' passwd.back //There is no space after option -i

[root@localhost desktop]# ls
manifest.txt passwdback1

-f When there are multiple items to be edited, you can put the editing command into a script, and then use sed with the -f option
[root@localhost desktop]# cat s.sed
s/bin/a/g
s/ ftp /b/g
s/mail/c/g
[root@localhost desktop]# sed -f s.sed passwd | less

#command part:
'[address1,address2][function][parameter(tag)]'

Addressing: /[0-9]+/ Use it as the address standard to execute the following awk content or sed content
Line address is optional for any command, it can be a pattern, or by slash, line number or line A regular expression enclosed in addressing symbols, most sed commands accept two addresses separated by commas, some commands only accept a single line address

命令还可以用大括号进行分组,第一个命令可以和大括号放在同一行,但是右大括号必须自己一行

Addressing method 1. Number 2. Regular
number (line number): decimal number
1 single line
#sed -n '1p' passwd p is the print function

    1,3 范围 从第一行到第三行      
        #sed -n '1,3p' passwd

     2,+4   匹配行后若干行         

     4,~3  从第四行到下一个3的倍数行

     1~3    第一行起每间隔三行的行

     $  尾行

     1! 除了第一行以外的行

正则:
   正则必须用//包裹起来
   扩展正则需要用 -r 参数或转移

#范例:
#cat file.txt
.TS
Beijing,CN
.TE
Shanghai,CN
guangzhou,CN
shenyang,CN
#sed ‘/Beijing/s/CN/China/' file.txt

#delete all lines#d
#delete
only the first line
#1d #use
the addressing symbol $ to delete the last line
#$d
#delete blank lines, the regular expression must be enclosed in slashes//
#/^$/d
#Delete tbl marked with .TS and .TE input
#/^.TS/,/^.TE/d #Delete
all lines from the fifth line to the end
#5,$d #Use
line address and mode address
mixed #sed '1 ,/^$/d' file.txt #delete
lines other than those lines
#1, 5!d

#Group command
/^.TS/,/^.TE/{
s/CN/China/
s/Beijing/BJ/
}
sed '2, 3s{/cn/china/;s/a/b/}' file. txt Two substitutions in the same range can be enclosed in curly brackets with a semicolon in between

#Function : Additions,
deletions and changes:
a after inserting
c to replace
i before inserting

d deletes all contents of the pattern space and then reads the next line into the pattern space

#Replacement: [address ]s/pattern /replacement /flags
flags flags are:
n can be 1-512, which means that the nth occurrence is replaced
g global change
p print the content of the pattern space
w file write to a file file In
i ignore case
[root@wing ~]# echo Aaaaba | sed -r 's/a. /B/'
AB
[root@wing ~]# echo Aaaaba | sed -r 's/a.
/B/i '
B

  s 字符串替换 s/old/new/

#When replacing, you can replace / with other symbols, such as =
#sed -n 's/root/ABCDEF/p' /etc/passwd
ABCDEF:x:0:0:root:/root:/bin/bash

The following characters in the replacement part have special meanings:

    & 用正则表达式匹配的内容进行替换
    \n 回调参数
    \(\)保存被匹配的字符以备反向引用\N时使用,最多9个标签 标签顺序从左至右

    #cat test1
    first:second
    one:two
    #sed ‘s/\(.*\):\(.*\)/\2:\1/' test1
    second:first
    two:one

y character replacement (deformation) y is to replace the entire string s can also be but s is a matching
r read the content from the file to the end of the specified line
#sed'2r b.txt' a.txt will b.txt file The content is read after line 2 of the file a.txt

#Working mode: Introduction to mode space and hold space
Mode space: Initialized to be empty, after processing a line, it will be automatically output to the screen and the mode space will be cleared.
: Change the content that matches the content and use the changed content as the next content to be changed
: After the first line is processed, print out the processed content, and change the pattern space to the second line to process
#Replacement: pattern space and Hold space (temporary space)
h Overwrite the content of the pattern space into the hold space
H Add the content of the pattern space to the hold space
g Overwrite the content of the hold space to the pattern space
G Add the content of the hold space to the pattern space
x Swap pattern Contents of space and hold space
d delete all contents of pattern space, and then read the next line into pattern space
reversal output Reverse directly with tac
$ sed '1!G;h;$!d' rev.txt
xyz
def
abc
$
Details
1! G;h;$!d
text
1
2
3
pattern space hold space
first line 1 1 next will cover
second line 2 2 next will cover
1 1
third line 3 3
2 2
1 1

#cat passwd |tac
:wq
1
1
1
1
1
o
xiaomi:x:1000:1000:xiaomi:/home/xiaomi:/bin/bash
tcpdump:x:72:72::/:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
emon:x:2:2:daemon:/sbin:/sbin/nologin

# cat test.sh 
1111111
2222222 
3333333
4444444
# sed  '{1h;2,3H;4G}'  ./test.sh 
1111111
2222222
3333333
4444444
1111111
2222222
3333333
# sed  '{1h;2x;3g;$G}'  ./test.sh 
1111111
1111111
2222222
4444444
2222222
保持空间:相当于粘贴板  里边内容不会主动被清空
保持空间:初始化为一个空行,也就是默认带一个\n,处理完后不会自动清除。

#Control flow
! Negative example of command: 1!d delete lines other than the first line
{} Command combination commands are separated by semicolons {1h;G} can be understood as another way of writing the -e parameter
= print line number (input line number, not the number of lines processed) Example: sed -n '2{=;p}' infile
n read next line into pattern space Example: '4{n;d}' delete line 5
N instead Append the next line to the pattern space, then apply the following command P to both the current line and the next line to
output the first part of the multiline pattern space, up to the first embedded newline. After the last command of the script is executed, the contents of the pattern space are automatically output.
The P command often appears after the N command and before the D command.
D Deletes the pattern space up to the first newline. It does not cause a new line of input to be read, instead it goes back to the top of the script and applies these instructions to the rest of the pattern space.

  这三个命令能建立一个输入、输出循环,用来维护两行模式空间,但是一次只输出一行。
  这个循环的目的是只输出模式空间的第一行,然后返回到脚本的顶端将所有的命令应用于模式空间的第二行。没有这个循环,当执行脚本中的最后一个命令时,模式空间中的这两行都将被输出。
  # tac  将文本内容倒过来
  # cat a.txt |tac
   删除文件倒数第二行
   #sed 'N;$!P;D' a.txt
    N将下一行追加到模式空间中 现在模式空间中会有 第一行和第二行
    交给P来进行   P将 第一行输出 出来 
    交给D来进行  D将 第一行删除 然后重新执行子命令  然后在来一边
    这样循环如果只有N;P;D   内容将不会该变什么 因为他可以循环到底
    在p前边加$!  是为了取反 执行最后一行之外的行     N将最后一行和倒数第二行放里边了
    P执行时 不执行最后一行直接略过交给D执行  然后他把第一行删除掉  内容中的第二行就被删除了
    然后再循环一次   因为后边没有内容了 N不再执行 P将第一行输出  粘贴板没有内容了  D也不执行  

   删除文件最后两行
   sed 'N;$!P;$!D;$d' a.txt

Exercise:
Insert the first row after each even row

$ sed  '1h;0~2G' a.txt
11111111
22222222
11111111
33333333
44444444
11111111
55555555
66666666
11111111

#Script method
-f parameter to refer to the script (no spaces, tabs or other text at the end of the script)

cat sed.sh

2,4d
s/777/seker/
s/999/seker&seker/
# sed -f sed.sh test.txt 
1111111
5555555
6666666
seker7777
8888888
seker999seker9999

#Specify the interpreter as sed in the script

cat sed.sh

#!/bin/sed -f
2,4d
s/777/seker/
s/999/seker&seker/

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325234731&siteId=291194637