shell script text three musketeers sed
1. Sed editor
1.1 Overview of sed
- The sed editor is a stream editor that edits the data stream based on a pre-provided set of rules before the editor processes the data.
- The sed editor can process data in a data stream according to commands, which are either entered from the command line or stored in a command text file.
1.2sed workflow
The sed workflow mainly includes three processes of reading, executing and displaying:
- Read: sed reads a line from the input stream (file, pipe, standard input) and stores it in a temporary buffer (also known as the pattern space)
- Execution: By default, all sed commands are executed sequentially in the pattern space. Unless a line address is specified, the sed command will be executed sequentially on all lines
- Display: Send the modified content to the output stream. After sending the data, the pattern space will be cleared
1.3 sed basic law
sed -e '操作' 文件1 文件2
sed -n -e '操作' 文件1 文件2
sed -f 脚本文件 文件1 文件2
sed -i -e '操作' 文件1 文件2
1.4 sed common options
-e或--expression=多点编辑
-f或--file=:表示用指定的脚本文件来处理输入的文件
-h或--help:显示帮助
-n:不输出模式空间内容到屏幕,即不自动打印,加p,又恢复自动打印
-i:备份文件并原处编辑
-r:使用扩展正则表达式
1.5Common operations of sed command
s:替换,替换指定符。
d:删除,删除选定的行
a:增加,在当前行下面增加一行指定内容
i:插入,在选定的行上面插入一行指定内容
c:替换,将选定行替换为指定内容
Y:字符转换,转换前后的字符长度必须相同
p:打印,如果同时指定行,表示打印指定行,如果不指定行,则表示打印所有内容,如果有非打印字符,则以ascll码输出。其中通常与_n选项一起使用
=:打印行号
l:打印数据流中的文本和不可打印的 ASCLL字符(比如结束符s,制表符\t)\
2. Use of sed command
2.1 Print content
- sed' 'input a line in interactive mode and automatically print the same line
- sed -n ' 'Enter a line in interactive mode, turn off automatic printing
Example 1: Print all content
Example 2: View the file and print all the content
Example 3: Print the content of the specified line
Example 4: According to the address, print the specified multi-line content
Example 5: Exit after printing multiple lines
Example 6: Print the last line, using wildcard $
Example 7: Support for regular expressions
Example 8: Filter keywords
Example 9: Keyword Lookup
Example 10: Print odd and even lines of text
2.2 Deleting content
Example 1: Delete the specified row
Example 2: Delete specified multiple lines
Example 3: Delete empty lines in a file
Example 4: Delete the line at the end of the specified character and negate it
Example 5: Back up content before deleting
2.3 Insert content
Example 1: Insert after specified row
Example 2: Insert a blank line, modify the newline of the file, and add a \
Example 3: Replace
2.4 Reconciliation
2.5 Search Alternatives
格式:sed 行范围 s/旧字符串/新字符串/替换标记
Replace tags:
- Number: Indicates which matching place the new string will replace
- g: Indicates that the new string will replace all matching places
- p: print lines matching the substitution command, used with -n
- w file: write the replacement result to the file
Example 1: Modify the configuration file of selinux booting without self-starting
[root@yxp opt]#sed -i 's/SELINUX=enabled/SELINUX=disabled/' /etc/selinux/config
Example 2: Modify multiple lines, use r and -e
[root@yxp opt]#sed -ri -e 's/SELINUX=disabled/SELINUX=enabled/' /etc/selinux/config -e 's/SELINUXTYPE=targeted/SELINUXTYPE=111/' /etc/selinux/config
Example 3; modify the global, followed by g
[root@yxp opt]#sed -i 's/root/admin/g' passwd
2.6 Group call
Example 1:
Example 2: Extract IP address
[root@yxp ~]#ifconfig ens33 |sed -rn '2s/.*(inet) ([0-9.]+) (netmask) ([0-9.]+) (broadcast) ([0-9.]+).*/\2/p'
192.168.59.102
Example 3: @ and / have the same effect