补充知识:

一.sed流编辑器:实现对文本的增删改查

1.改:语法sed '/匹配条件/s/old/new/g' file

[root@ns ~]# sed 's/dhcp/static/g' /etc/sysconfig/network-scripts/ifcfg-eth1 ##只是显示,不修改

clip_image002

[root@ns ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth1

clip_image004

[root@ns ~]# sed -i 's/dhcp/static/g' /etc/sysconfig/network-scripts/ifcfg-eth1 ##只修改,不显示

[root@ns ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth1 ##验证

clip_image006

[root@ns ~]# vim ip ##编写测试文件

IP1=static

IP2=static

IP=static

:wq

clip_image008

[root@ns ~]# sed -i 's/dhcp/static/g' ip ##将所有的dhcp替换为static

clip_image010

[root@ns ~]# sed -i '/^IP1/s/static/dhcp/g' ip ##将IP1开头的行替换

clip_image012

[root@ns ~]# sed -i '2s/static/dhcp/g' ip ##指定特定行号2行替换

clip_image014

[root@ns ~]# cat -n /etc/selinux/config ##查看并显示行号

clip_image016

[root@ns ~]# sed -i '7s/disabled/enforcing/g' /etc/selinux/config ##开启selinux

clip_image018

[root@ns ~]# sed -i '7s/disabled/enforcing/g' /etc/selinux/config

clip_image020

2.删:sed '/表达式/d' file

[root@ns ~]# vim ip ##添加空行

clip_image022

[root@ns ~]# sed '/^$/d' ip ##删除空行并显示在屏幕上

clip_image024

[root@ns ~]# sed -i '/IP1/d' ip ##删除包含IP1的行

clip_image026

[root@ns ~]# sed -i '/^IP2/d' ip ##删除以IP2开头的行

clip_image028

[root@ns ~]# sed -i '2d' ip ##删除第二行

clip_image030

3.增:sed '/表达式/a "需要添加的文字"' file

[root@ns ~]# sed 'a IP3=static' ip ##每一行后都加上IP3=static

clip_image032

[root@ns ~]# sed '3a IP3=static' ip ##只在第3行后加上IP3=static,并显示不修改

clip_image034

[root@ns ~]# sed '3i IP3=static' ip ##只在第3行前加上IP3=static,显示不修改

clip_image036

[root@ns ~]# sed -i '3a IP3=static' ip ##修改,不显示

clip_image038

[root@ns ~]# sed -i '/^IP3/a "test add"' ip ##在以IP3开头的行后添加

clip_image040

4.查:sed -n '/表达式/p' file

[root@ns ~]# sed -n '2p' /etc/hosts ##查看第二行

clip_image042

[root@ns ~]# sed -n '/www/p' /var/named/chroot/var/named/linuxfan.cn.zone ##查看包含www的解析记录

clip_image044

[root@ns ~]# sed -n '/.100$/p' /var/named/chroot/var/named/linuxfan.cn.zone ##查看以.100结尾的行

clip_image046

二.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

clip_image048

[root@ns ~]# uniq ip ##去重,但是只是连续重复的行

clip_image050

[root@ns ~]# uniq -c ip ##去重,同时显示重复的次数

clip_image052

[root@ns ~]# uniq -d ip ##只显示重复的行

clip_image054

2.sort排序:

[root@ns ~]# yum -y install sysstat ##安装系统状态查看工具

clip_image056

[root@ns ~]# sar -u 2 50 >cpu_load.txt ##查看cpu使用状态每2秒一次共50次

[root@ns ~]# sort -k 8 cpu_load.txt ##以第八列进行排序

clip_image058

[root@ns ~]# sort ip |uniq -c ##一般先使用sort排序,再uniq能把所有重复都去掉

clip_image060

3.date修改或显示时间:

[root@ns ~]#

[root@ns ~]# man date ##查看帮助

SYNOPSIS

date [OPTION]... [+FORMAT] ##显示系统时间

date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]] ##修改系统时间

:q

clip_image062

常用的组合:

[root@ns ~]# date +%F-%T ##查看时间,很符合国人的习惯

clip_image064

[root@ns ~]# date +%F ##在备份或其他以天为单位中常用

clip_image066

[root@ns ~]# date +%s ##从1970-01-01:00:00:00开始到现在的秒数

clip_image068

[root@ns ~]# date +%F -d '2 weeks ago' ##显示两周前的日期,也可以使用days

clip_image070

[root@ns ~]# date +%F -d '3 days' ##三天后的日期

clip_image072

[root@ns bin]# date +%F -d "2004-02-29 16:21:42" ##指定特定的时间

clip_image074

[root@ns bin]# date +%F -d "2004-02-29 16:21:42 + 3days"

clip_image076

[root@ns bin]# date +%F -d '+3days' ##3天后

clip_image078

[root@ns bin]# date +%F -d '-3days' ##3天前

clip_image080

[root@ns bin]# date +%F -d "next mon" ##下周一

clip_image082