Linux--sed detailed explanation of file processing three swordsmen--multiple cases


sed editor

Overview

  • Sed is a stream editor, the stream editor will edit the data stream based on a set of rules provided in advance before the editor processes the data
  • The sed editor can process the data in the data stream according to commands, which are either input from the command line or stored in a command text file
  • The working process of the stream editor can be understood as: it is executed sequentially from top to bottom, just like pipeline, executed line by line

work process

Mainly include the following three processes

Read

  • Sed reads a line of content from the input stream (file, pipe, standard input) and stores it in a temporary buffer (also known as pattern space)

carried out

  • By default, all sed commands are executed sequentially in the pattern space
  • Unless the address of the line is specified, the sed command will be executed in sequence on all lines

display

  • Send the modified content to the output stream
  • After sending the data, the pattern space will be emptied
  • Before all the file contents are processed, the above process will be repeated until all the contents are processed

Command format

#例:
sed -e '操作' 文件1 文件2 ...
sed -n -e '操作' 文件1 文件2 ...
sed -f 脚本文件 文件1 文件2 ...
sed -i -e '操作' 文件1 文件2 ...


sed -e 'n{
操作1    
操作2
...
}' 文件1 文件2 ...

Common options

Options Explanation
-e or --expression Indicates that the specified command is used to process the input text file. When there is only one operation command, it can be omitted. Generally, it is used when executing multiple operation commands
-f or --file Indicates that the specified script file is used to process the input text file
-h or --help Show help
-n, --quiet or silent Sed editor output is prohibited, but it can be used with the p command to complete the output
-i Modify the target text file directly

Common operations

Options Explanation
s Replace: replace the specified character
d Delete: delete the selected row
a Increase: add a line of specified content below the current line
i Insert: Insert a line of specified content on the selected line
c Replace: replace the selected line with the specified content
and Character conversion; note that the length of characters before and after conversion must be the same
p Print; if you specify a line at the same time, it means to print the specified line; if you do not specify a line, it means to print all the content; if there are non-printing characters, it will be output in ASCII; and it is usually used in conjunction with the "-n" option
= Print line number
l Print the text and non-printable ASCII characters in the data stream (such as the terminator $, the tab character \t)

Example

Print content

sed -ne 'p' zxc1

mark
mark

sed -n '=' zxc1

mark

sed -n 'l' zxc1

mark

sed -n '=;p' zxc1

mark

[root@localhost shell]# sed -n '
> p
> =
> ' zxc1

mark

Use address

  • The sed editor has 2 addressing modes:
    • Represent the row interval in numeric form
    • Use text mode to filter out lines
[root@localhost shell]# sed -n '1p' zxc1        ##打印第一行
one
[root@localhost shell]# sed -n '$p' zxc1        ##打印最后一行
twelve
[root@localhost shell]# sed -n '1,3p' zxc1        ##打印输出第一行至第三行的内容,可在3前面加一个+符号
one
two
three
[root@localhost shell]# sed -n '3,$p' zxc1        ##打印从第三行至最后一行的内容
three
four
five
six
seven
eight
nine
ten
eleven
twelve
[root@localhost shell]# sed 5q zxc1        ##q用法:打印前5行内容后退出
one
two
three
four
five
sed -n 'p;n' zxc1

sed -n 'n;p' zxc1

mark

sed -n '2,${n;p}' zxc1

mark

sed -n '/user/p' /etc/passwd

mark

sed -n '/^a/p' /etc/passwd

mark

sed -n '/bash$/p' /etc/passwd

sed -n '/ftp\|root/p' /etc/passwd

mark

sed -n '2,/nobody/p' /etc/passwd

sed -n '2,/nobody/=' /etc/passwd

mark

sed -n '2,/nobody/=;2,/nobody/p' /etc/passwd

mark

[root@localhost shell]# sed -nr '/ro{1,}t/p' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
##-r表示支持正则表达式
##匹配一次以上的o,就能匹配到root

Delete row

sed 'd' zxc1

sed '3d' zxc1

sed '2,4d' zxc1

mark

mark

[root@localhost shell]# sed '/^$/d' zxc1
##用作删除空行
[root@localhost shell]# sed '/nologin$/d' /etc/passwd
##删除以nologin为结尾的内容

[root@localhost shell]# sed '/nologin$/!d' /etc/passwd
##"!"表示取反操作

mark

sed -n 'p' zxc2

sed '/2/,/3/d' zxc2

mark

cat zxc2

sed '/10/,/3/d' zxc2 

mark

Replacement mark

##格式:
行范围 s/旧字符串/新字符串/替换标记

4 types of replacement tags:

  • Number: Indicate which match the new string will replace
  • g: Indicates that the new string will replace all matches
  • p: Print the line matching the replace command, used with -n
  • w file: write the result of the replacement to the file
  • Example:
sed -n 's/root/admin/p' /etc/passwd

sed -n 's/root/admin/2p' /etc/passwd

mark

sed -n 's/root/admin/gp' /etc/passwd

mark

sed -n 's/root//gp' /etc/passwd

mark

[root@localhost shell]# sed '1,20 s/^/#/' /etc/passwd

mark

sed '/^root/ s/$/#/' /etc/passwd

mark

mark

cat zxc2

sed -f zxc3 zxc2

mark

sed '1,20w, out.txt' /etc/passwd > dev/null

mark

sed '1,20w out.txt' /etc/passwd > /dev/null 

cat out.txt

mark

insert

mark

sed '1,3a ABC' 123.txt

mark

sed '1i ABC' 123.txt

mark

sed '5r /etc/resolv.conf' 123.txt

mark

sed '/root/{H;d};$G' /etc/passwd
##包含root的行剪切到末尾,H表示复制到剪切板,G表示粘贴到指定行后

mark

Guess you like

Origin blog.csdn.net/weixin_51486343/article/details/111753171