SHELL脚本 sed基础练习

1、正则表达式

1、删除df -hT命令行首及所有tmpfs的行

[root@vm01 ~]# df -Th | sed '/tmpfs/d'
文件系统                类型      容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root xfs        17G  1.8G   16G   11% /
/dev/sda1               xfs       497M  130M  368M   26% /boot

2、修改ssh配置文件,要求如下:

1) 修改默认端口为55555

2) 关闭DNS反解

3) 禁止root用户远程登录

[root@vm01 ~]# sed -e '/#Port/c \Port 55555' -e '/#UseDNS yes/c \UseDNS no' -e '/#PermitRootLogin yes/c \PermitRootLogin no' /etc/ssh/sshd_config > test01.txt 
[root@vm01 ~]# grep test01.txt Port
grep: Port: 没有那个文件或目录
[root@vm01 ~]# grep  ^Port test01.txt 
Port 55555
[root@vm01 ~]# grep  ^PermitRootLogin test01.txt 
PermitRootLogin no
[root@vm01 ~]# grep  ^UseDNS test01.txt 
UseDNS no

3、将selinux配置文件中SELINUX=enforcing修改为SELINUX=disabled

[root@vm01 ~]# sed '/SELINUX=enforcing/c SELINUX=disabled' /etc/selinux/config > test01.txt
[root@vm01 ~]# grep ^SELINUX test01.txt 
SELINUX=disabled
SELINUXTYPE=targeted 

4、安装httpd,并修改其默认端口为8080

[root@vm01 ~]# sed '/Listen 80/c \Listen 8080' /usr/local/httpd24/conf/httpd.conf > test01.txt 
[root@vm01 ~]# grep ^Listen test01.txt 
Listen 8080

5、在/etc/hosts文件中添加主机名解析

[root@vm01 ~]# sed '$a \10.1.1.1  www.linux.com' /etc/hosts > test01.txt 
[root@vm01 ~]# cat test01.txt 
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

192.168.110.221	www.linux.com
10.1.1.1  www.linux.com

6、在/var/log/messages日志文件中获取指定时间范围内的日志

[root@vm01 ~]# sed -n '/Jan  5 17:06:44/,/Jan  5 17:06:47/p' /var/log/messages > test01.txt 
[root@vm01 ~]# less test01.txt 

7、任意创建文件写入4行内容,将其第2, 4行注释

[root@vm01 ~]# sed -e '2 s/^/#/' -e '4 s/^/#/' test01.txt 
aa
#bb
cc
#dd

8、显示文件/etc/passwd第五行内容

[root@vm01 ~]# sed -n '5p' /etc/passwd
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

9、删除历史命令前所有空白字符

[root@vm01 ~]# history | sed 's/^ *//' | head
1  ping baidu.com
2  ip a
3  bash rec.sh
4  yum list all
5  yum replist
6  yum repolist
7  init 6
8  systemctl status firewalld.service 
9  ntpdate 120.25.108.11
10  cd /etc/yum.repos.d/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值