补充知识:
一.sed流编辑器:实现对文本的增删改查
1.改:语法sed '/匹配条件/s/old/new/g' file
[root@ns ~]# sed 's/dhcp/static/g' /etc/sysconfig/network-scripts/ifcfg-eth1 ##只是显示,不修改
[root@ns ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth1
[root@ns ~]# sed -i 's/dhcp/static/g' /etc/sysconfig/network-scripts/ifcfg-eth1 ##只修改,不显示
[root@ns ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth1 ##验证
[root@ns ~]# vim ip ##编写测试文件
IP1=static
IP2=static
IP=static
:wq
[root@ns ~]# sed -i 's/dhcp/static/g' ip ##将所有的dhcp替换为static
[root@ns ~]# sed -i '/^IP1/s/static/dhcp/g' ip ##将IP1开头的行替换
[root@ns ~]# sed -i '2s/static/dhcp/g' ip ##指定特定行号2行替换
[root@ns ~]# cat -n /etc/selinux/config ##查看并显示行号
[root@ns ~]# sed -i '7s/disabled/enforcing/g' /etc/selinux/config ##开启selinux
[root@ns ~]# sed -i '7s/disabled/enforcing/g' /etc/selinux/config
2.删:sed '/表达式/d' file
[root@ns ~]# vim ip ##添加空行
[root@ns ~]# sed '/^$/d' ip ##删除空行并显示在屏幕上
[root@ns ~]# sed -i '/IP1/d' ip ##删除包含IP1的行
[root@ns ~]# sed -i '/^IP2/d' ip ##删除以IP2开头的行
[root@ns ~]# sed -i '2d' ip ##删除第二行
3.增:sed '/表达式/a "需要添加的文字"' file
[root@ns ~]# sed 'a IP3=static' ip ##每一行后都加上IP3=static
[root@ns ~]# sed '3a IP3=static' ip ##只在第3行后加上IP3=static,并显示不修改
[root@ns ~]# sed '3i IP3=static' ip ##只在第3行前加上IP3=static,显示不修改
[root@ns ~]# sed -i '3a IP3=static' ip ##修改,不显示
[root@ns ~]# sed -i '/^IP3/a "test add"' ip ##在以IP3开头的行后添加
4.查:sed -n '/表达式/p' file
[root@ns ~]# sed -n '2p' /etc/hosts ##查看第二行
[root@ns ~]# sed -n '/www/p' /var/named/chroot/var/named/linuxfan.cn.zone ##查看包含www的解析记录
[root@ns ~]# sed -n '/.100$/p' /var/named/chroot/var/named/linuxfan.cn.zone ##查看以.100结尾的行
二.uniq+sort+date
1.uniq:去重
[root@ns ~]# vim ip ##编写测试文件
IP1=dhcp
IP2=dhcp
IP2=dhcp
IP3=static
"test add"
"test add"
IP2=dhcp
12
12
12
12
12
12
12
13
13
13
13
[root@ns ~]# uniq ip ##去重,但是只是连续重复的行
[root@ns ~]# uniq -c ip ##去重,同时显示重复的次数
[root@ns ~]# uniq -d ip ##只显示重复的行
2.sort排序:
[root@ns ~]# yum -y install sysstat ##安装系统状态查看工具
[root@ns ~]# sar -u 2 50 >cpu_load.txt ##查看cpu使用状态每2秒一次共50次
[root@ns ~]# sort -k 8 cpu_load.txt ##以第八列进行排序
[root@ns ~]# sort ip |uniq -c ##一般先使用sort排序,再uniq能把所有重复都去掉
3.date修改或显示时间:
[root@ns ~]#
[root@ns ~]# man date ##查看帮助
SYNOPSIS
date [OPTION]... [+FORMAT] ##显示系统时间
date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]] ##修改系统时间
:q
常用的组合:
[root@ns ~]# date +%F-%T ##查看时间,很符合国人的习惯
[root@ns ~]# date +%F ##在备份或其他以天为单位中常用
[root@ns ~]# date +%s ##从1970-01-01:00:00:00开始到现在的秒数
[root@ns ~]# date +%F -d '2 weeks ago' ##显示两周前的日期,也可以使用days
[root@ns ~]# date +%F -d '3 days' ##三天后的日期
[root@ns bin]# date +%F -d "2004-02-29 16:21:42" ##指定特定的时间
[root@ns bin]# date +%F -d "2004-02-29 16:21:42 + 3days"
[root@ns bin]# date +%F -d '+3days' ##3天后
[root@ns bin]# date +%F -d '-3days' ##3天前
[root@ns bin]# date +%F -d "next mon" ##下周一
转载于:https://blog.51cto.com/liuqicheng/1874382