$# 参数的个数
unit sed awk
sed :stream editor 一次处理一行内容;把当前行存储在临时缓冲区;处理完后输送到屏幕
sed [] '命令' 文件
p 显示
d 删除
a 添加
c 替换
i 插入(修改原文件)
sed -n '/\:/p' /etc/fstab 里面的p是显示意思
sed -n '/^#/p' /etc/fstab 显示#开头
sed -n '2,6p' /etc/fstab 显示2-6行
sed -n '2,6!p' /etc/fstab 不显示2-6行
sed -n '2p,6p' /etc/fstab 显示2 6
sed '/^#/d' /etc/fstab 删除#开头的行
sed '/^$/d' 删除空行
sed '1,4d' 删除1-4
替换
sed 's/hello/hello world/g' westos
sed '/hello/aworld' westos 换行添加
sed 's/hello\nworld/g' westos
sed '/hello/chello world' westos
sed '/a/ib\nc' 在a前面插入b再写入c
b
c
a
sed 's/\//#/g' /etc/fstab /替换为#
sed -i 's/nologin/westos/g' westos
自动安装与修改端口的脚本
#!/bin/bash
yum install httpd -y >/dev/null
systemctl enable httpd
systemctl start httpd
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-port=8080
firewall-cmd --reload
semanage port -a -t http_port_t -p tcp 8080
sed -i '/Listen 80/cListen 8080' /etc/httpd/conf/httpd.conf
egrep 'Listen 8080' /etc/httpd/conf/httpd.conf
任意修改端口
yum install httpd -y >/dev/null
systemctl enable httpd
systemctl start httpd
firewall-cmd --permanent --add-service=http >/dev/null
firewall-cmd --permanent --add-port='$1'/tcp >/dev/null
firewall-cmd --reload
sed -i '/Listen 80/cListen '$1'' /etc/httpd/conf/httpd.conf &>/dev/null
semanage port -a -t http_port_t -p tcp $1 &>/dev/null
systemctl restart httpd >/dev/null
egrep 'Listen '$1'' /etc/httpd/conf/httpd.conf