Shell-sed editor

One, sed

1. Overview of sed

The sed editor can process the data in the data stream according to commands, which are either entered from the command line or stored in a command text file.

2. Work flow

The workflow of sed mainly includes three processes of reading, executing and displaying:

  • 1. 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)
  • 2. Execution: 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 sequentially on all lines.
  • 3. 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.

Before all the file contents are processed, the above process will be repeated until all the contents are processed.
Note: By default, all sed commands are executed in the pattern space, so the input file will not change in any way, unless redirection is used to store the output.

3. 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

4. Common options

-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, when executing multiple operation commands, use
-f or--file=: Indicates that the specified script file is used. Process the input text file.
-h or--help: Display help.
-n,--quiet or silent: Disable the output of the sed editor, but can be used with the p command to complete the output.
-i: directly modify the target text file.

Insert picture description here
Insert picture description here
Insert picture description here

5. Common operations

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 above the selected line.
c: Replace, replace the selected line with the specified content.
y: Character conversion, 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 code. It is usually used with the "-n" option.
=: Print the line number.
l (lowercase L): print the text in the data stream and non-printable ASCII characters (such as the terminator $, the tab character \t)
Insert picture description here

sed -n '=' 1.txt        #显示行号

Insert picture description here

sed -n 'l' 1.txt      #l (小写L):打印数据流中的文本和不可打印的ASCII字符(比如结束符$、制表符\t)

Insert picture description here

6. Use address to find

The sed editor has 2 addressing modes:
1. Express the line interval in digital form

sed -n '1p' 1.txt         #显示第1行内容
sed -n '$p' 1.txt         #显示最后一行内容
sed -n '1,3p' 1.txt       #显示第1行到第3行内容
sed -n '3,$p' 1.txt       #显示第3行到最后一行内容
sed -n '3,+3p' 1.txt      #显示第3行加3行内容
sed -n 'p;n' 1.txt        #显示奇数行内容
sed -n 'n;p' 1.txt        #显示偶数行内容

Insert picture description here
Insert picture description here
2. Use text mode to filter trips

sed -n ' /user/p' /etc/passwd        #//显示user的行进行打印,区分大小写
sed -n ' /^a/p' /etc/passwd          #显示以a开头的行进行打印
sed -n '/bash$/p' /etc/passwd        #显示bash结尾的行
sed -n '/ftp\|root/p' /etc/passwd    #显示包含ftp或者root的行
sed -n '2,/nobody/p' /etc/passwd     #从第二行开始打印到包含nobody的行
sed -nr '/ro{1,}t/p' /etc/passwd     #-r表示支持正则表达式

Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

7, delete rows

sed 'd' 1.txt                      #不指定行号,全部删除
sed '2d' 1.txt                     #删除第三行
sed '2,4d' 1.txt                 #删除2-4行
sed '/^$/d' 1.txt                #删除空行
sed '/nologin$/d' /etc/passwd      #删除包含nologin字符的行
sed '/nologin$/!d' /etc/passwd     #不删除包含nologin的行
sed '/o/,/f/d' 1.txt

Insert picture description here

Insert picture description here

8. Replace

行范围 s/旧字符 /新字符 /替换标记

4 types of replacement marks
Number: indicates the new character string will replace the matching place
g: indicates that the new character will replace all the matching places
p: print the line that matches the replacement command, use with -n
w file: replace Write the results to a file

sed -n 's/root/admin/p' /etc/passwd    #将匹配行的第一个root更改为admin
sed -n 's/root/admin/2p' /etc/passwd   #将匹配行的第二个root更改为admin
sed -n 's/root/admin/gp' /etc/passwd   #将匹配行的所有root更改为admin 
sed '1,20 s/^/#/' /etc/passwd          #1-20行行首添加#号
sed '/^root/ s/$/#/' /etc/passwd       #在以root开头的行,行尾添加#号
sed -f 1.sed 1.txt                     #-f以指定的脚本文件来处理输入文件
sed '1,3w out.txt' 1.txt
sed -n 's/\/bin\/bash/\/bin\/srs/p' /etc/passwd

Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

9. Insert

sed '/2/two 2' 1.txt  #将含有two的行,替换为2
sed '1,2a 2' 1.txt   #在第一行到第二行,行下插入2
sed '5r /etc/resolv.conf' 1.txt  #在第五行后导入该文件内容
sed '/two/{H;d};$G' 1.txt	#将包含root的行剪切到末尾,H表示复制到剪切板,G表示粘贴到指定行后

Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/s15212790607/article/details/114739059