linux中sed用法

$#  参数的个数

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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值