Article Directory
1. Introduction to sed
Sed is a kind of stream editor, it is a very medium tool in text processing, it can be used perfectly with regular expressions, and its functions are extraordinary. During processing, the currently processed line is stored in a temporary buffer called "pattern space", and then the content in the buffer is processed with the sed command. After the processing is completed, the content of the buffer is sent to the screen. Then process the next line, and repeat this way until the end of the file. The content of the file does not change unless you use redirection to store the output. Sed is mainly used to automatically edit one or more files; simplify repeated operations on files; write conversion programs, etc.
2. Introduction to sed command
格式:
sed [选项] 命令 处理对象
sed [选项] '表达式1; 表达式2' 处理对象
|
|
---|---|
|
Show only processing results |
|
Save the modified content to the file |
|
Allow multiple commands to be executed on the same line |
|
Use script files to process file |
|
|
---|---|
|
Display line (line of print template block) |
|
Delete row |
|
Insert line (insert text below the current line) |
|
Insert line (insert text above the current line) |
|
Replace line |
|
Replacement character |
|
Write and append the template block to the end of the file |
|
Consolidate files (read lines from file) |
|
drop out |
|
Indicates that the following commands act on unselected rows |
|
Print current line number |
|
|
---|---|
|
Match line start |
|
End of matching line |
|
Matches any character other than a newline |
|
Match 0 or more characters |
|
Save search characters to replace other characters |
|
Match the beginning of a word |
|
Match the end of a word |
|
m times |
|
At least m times |
|
mn times |
|
0-n times |
|
How many times the text appears |
3. Examples of sed usage
p display line
-n 5p #显示第五行
-n 3,5p #显示3-5行
-ne '3p;5p' #显示3和5行
-ne '5,$p' #5-最后一行
-n '/^ro/p' #显示以ro开头的行
d delete
sed 5d passwd #删除第5行
sed '/^ro/d' passwd #把ro开头的行删除
sed '/^ro/!d' passwd #除了ro开头的行都删除
sed '3,$d' passwd #删除第3行之后
sed '/^$/d' passwd #删除空白行
a Insert (insert text below the current line)
sed "5a hello westos" passwd #第5行后添加行
sed "/root/a hello westos" passwd #有root的行后添加行
sed '$a hello westos' passwd #最后一行后添加行
sed '1,5a hello westos' passwd #1-5行,每行后添加行
sed -e '1a hello westos' -e '5a hello' passwd #第一行和第5行后添加行
i Insert (insert text above the current line)
sed '3i zy' passwd #在第3行前插入行
sed '1,3i zy' passwd #在1-3行,每行前插入行
sed '/lp/i zy' passwd #在有lp这一行前插入行
c replace
sed '3c hello zy' passwd #第3行替换
sed '/^root/c hello zy' passwd #root开头的行替换
sed '/sbin/c hello zy' passwd #有sbin的行替换
w write file
sed '3w w_file' passwd #把文件passwd的第三行写入到w_file中
sed '3,5w w_file' passwd #把文件passwd的3-5行写入到w_file中
sed '/var/w w_file' passwd #把文件passwd有var的行写入到w_file中
r Integration file
(after line number)
sed '3r r_file' passwd #把文件r_file整合在passwd第3行之后
sed '$r r_file' passwd #把文件r_file整合在passwd最后
s replacement character
sed 's/sbin/---/g' passwd #把全文的sbin替换为---
sed '3s/sbin/---/g' passwd #把第3行的sbin替换为---
sed '2,4s/sbin/---/g' passwd #把第2-4行的sbin替换为---
sed '2,4s/sbin/---/g;6s/sbin/+++/g' passwd #把第2-4行的sbin替换为---,第6行的sbin替换为+++
sed -e '/bin/,/adm/s/sbin/---/g' passwd #bin到adm之间的行的sbin替换为---
sed -e 's/\//--/g' passwd #把/替换为-(需要转义符号)
sed -e 's@/@777@g' passwd #把/替换为-(用@无需转义)
= Show line number
sed '=' passwd #显示行号(行号和内容不在同一列)
sed '$=' passwd #只显示最后一行的行号
sed '=' passwd | sed -e 'N;s/\n/ /g' #将行号和内容显示在同一列
sed -n '$=' passwd #统计行号
wc -l passwd #统计行号
4. Testing
Edit the script apache_port.sh
- Modify the http port number to the number followed by the script
- selinux is off
#!/bin/bash
#检测脚本后的端口号是否为空
[ -z $1 ] && {
echo "ERROR: Please input port following script !"
exit
}
#找到http主配置文件中的端口号那一行 并修改
sed "/^Listen/c Listen $1" -i /etc/httpd/conf/httpd.conf
#重启httpd服务
systemctl restart httpd
exit #退出