一、sed
sed:stream editor(流编辑器) :一次处理一行内容,处理时,把当前的行存储在临时缓冲区,处理完后,输送到屏幕
sed [参数] '命令' file
p ##显示
d ##删除
a ##添加
c ##替换
i ##插入
1)p:显示
sed -n '/\:/p' /etc/fstab ##显示带:的行
sed -n '/^#/p' /etc/fstab ##显示以#开头的行
sed -n '/^#/!p' /etc/fstab ##显示不以#开头的行
sed -n '2,6p' /etc/fstab ##显示2-6行
sed -n '2,6!p' /etc/fstab ##不显示2-6行
实验
[root@localhost mnt]# sed -n '/\:/p' /etc/fstab
# Created by anaconda on Wed May 7 01:22:57 2014
[root@localhost mnt]# sed -n '/^#/p' /etc/fstab
#
# /etc/fstab
# Created by anaconda on Wed May 7 01:22:57 2014
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
[root@localhost mnt]# sed -n '/^#/!p' /etc/fstab
UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 / xfs defaults 1 1
[root@localhost mnt]# sed -n '2,6p' /etc/fstab
#
# /etc/fstab
# Created by anaconda on Wed May 7 01:22:57 2014
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
[root@localhost mnt]# sed -n '2,6!p' /etc/fstab
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 / xfs defaults 1 1
2)d:删除
sed '/^UUID/d' /etc/fstab ##删除以UUID开头的行
sed '/^#/d' /etc/fstab ##删除以#开头的行
sed '/^$/d' /etc/fstab ##^$开头即是结尾,删除空行
sed '1,4d' /etc/fstab ##删除1-4行
实验:
[root@localhost mnt]# sed '/^UUID/d' /etc/fstab
#
# /etc/fstab
# Created by anaconda on Wed May 7 01:22:57 2014
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
[root@localhost mnt]# sed '/^#/d' /etc/fstab
UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 / xfs defaults 1 1
[root@localhost mnt]# sed '/^$/d' /etc/fstab
#
# /etc/fstab
# Created by anaconda on Wed May 7 01:22:57 2014
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 / xfs defaults 1 1
[root@localhost mnt]# sed '1,4d' /etc/fstab
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 / xfs defaults 1 1
3)a:添加
sed '/hello/aworld' westos ##在hello下一行添加world
sed 's/hello/hello world/g' westos ##在hello替换为hello world
sed 's/hello/hello\nworld/g' westos ##在hello替换为hello 换行 world
实验
[root@localhost mnt]# cat westos
hello world
hello
[root@localhost mnt]# sed '/hello/aworld' westos
hello world
world
hello
world
[root@localhost mnt]# sed 's/hello/hello world/g' westos
hello world world
hello world
[root@localhost mnt]# sed 's/hello/hello\nworld/g' westos
hello
world world
hello
world
4)c:替换
sed '/hello/chello world' westos ##将hello所在的行换为helloworld
实验:
[root@localhost mnt]# sed '/hello/chello world' westos
hello world
hello world
5)i:插入
sed '/hello/iworld\nwestos' westos ##在hello上面插入 world 换行 westos
[root@localhost mnt]# sed '/hello/iworld\nwestos' westos
world
westos
hello world
world
westos
hello
sed 's/\//#/g' /etc/fstab
6)-i:改变原文件内容,执行完成后原来文件也会被修改
sed -i 's/westos/redhat/' passwd
sed -i 's/westos/redhat/g' passwd ##全局替换
2、awk报告生成器
awk处理机制:根据模式一次从文件中抽取一行文本,对这行文本进行切片(默认使用空白字符作为分隔符)
1)awk的基本用法
一、基本输出
[root@localhost mnt]# cat test.sh
this | is | a | pig
$1 $2 $3 $4
awk '{print $0}' test ##$0表示输出一整行
awk '{print $1}' test
awk '{print $4}' test
awk '{print $1,$2}' test ##显示两个字段
实验:
[root@localhost mnt]# awk '{print $0}' test
awk: fatal: cannot open file `test' for reading (No such file or directory)
[root@localhost mnt]# awk '{print $0}' test.sh
this is a pig
[root@localhost mnt]# awk '{print $1}' test.sh
this
[root@localhost mnt]# awk '{print $2}' test.sh
is
[root@localhost mnt]# awk '{print $3}' test.sh
a
[root@localhost mnt]# awk '{print $4}' test.sh
pig
[root@localhost mnt]# awk '{print $1,$2}' test.sh
this is
二、指定分隔符
awk -F ":" '{print $1,$3}' /etc/passwd ##指定分隔符
实验
[root@localhost mnt]# cat test.sh
this is: a: pig
[root@localhost mnt]# awk -F ":" '{print $1,$3}' test.sh
this is pig
2).awk常用变量
FILENAME ##文件名
NR ##行号
NF ##列号
awk '{print FILENAME,NR}' /etc/passwd ##输出文件名,和当前操作的行号
awk -F: '{print NR,NF}' /etc/passwd ##输出每次处理的行号,以及当前以":"为分隔符的字段个数
总结:awk '{print "第NR行","有NF列"}' /etc/passwd
实验:
[root@localhost mnt]# awk '{print FILENAME,NR}' /etc/passwd
/etc/passwd 1
/etc/passwd 2
/etc/passwd 3
/etc/passwd 4
/etc/passwd 5
/etc/passwd 6
/etc/passwd 7
/etc/passwd 8
/etc/passwd 9
[root@localhost mnt]# awk -F: '{print NR,NF}' /etc/passwd
1 7
2 7
3 7
4 7
5 7
6 7
7 7
8 7
9 7
3)wak的一些简单命令
一、BEGIN 和 END
BEGIN{} ##读入第一行文本之前执行的语句,一般用来初始化操作
{}:逐行处理
END{} ##处理完最后以行文本后执行,一般用来处理输出结果
awk -F: 'BEGIN{print "REDHAT"} {print NR;print } END {print "WESTOS"}' file ##文件开头加REDHAT,末尾加WESTOS,打印行号和内容
实验
[root@localhost mnt]# cat file
this is a file
[root@localhost mnt]# awk -F: 'BEGIN{print "REDHAT"} {print NR;print } END {print "WESTOS"}' file
REDHAT
1
this is a file
WESTOS
awk -F: '/bash$/{print}' /etc/passwd ##输出以bash结尾的
实验
[root@localhost mnt]# awk -F: '/bash$/{print}' /etc/passwd
root:x:0:0:root:/root:/bin/bash
student:x:1000:1000:Student User:/home/student:/bin/bash
brian:x:1001:1002::/home/brian:/bin/bash
rob:x:1002:1003::/home/rob:/bin/bash
awk -F: 'NR==3 {print}' /etc/passwd ##输出第三行
实验
[root@localhost mnt]# awk -F: 'NR==3 {print}' /etc/passwd
daemon:x:2:2:daemon:/sbin:/sbin/nologin
awk -F: 'NR % 2 == 0 {print}' /etc/passwd ##偶数行
实验
awk -F: 'NR >=3 && NR <=5 {print }' /etc/passwd ##输出3-5行
实验
[root@localhost mnt]# awk -F: 'NR >=3 && NR <=5 {print }' /etc/passwd
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
awk 'BEGIN{i=0}{i+=NF}END{print i}' /etc/passwd ##统计文本总字段个数
实验
[root@localhost mnt]# awk 'BEGIN{i=0}{i+=NF}END{print i}' /etc/passwd
77
二、条件语句
#if单分支语句
awk -F: 'BEGIN{i=0}{if($7~/bash$/){i++}}END{print i}' /etc/passwd ##统计登录shell为bash的用户
[root@localhost mnt]# awk -F: 'BEGIN{i=0}{if($7~/bash$/){i++}}END{print i}' /etc/passwd
4
#if双分支
awk -F: 'BEGIN{i=0;j=0}{if($3<=500){i++}else{j++}}END{print i,j}' /etc/passwd ##统计uid小于等于500和大于500的用户个数
[root@localhost mnt]# awk -F: 'BEGIN{i=0;j=0}{if($3<=500){i++}else{j++}}END{print i,j}' /etc/passwd
33 11
三、循环语句
#for循环
awk 'BEGIN{for(i=1;i<=5;i++){print i}}' ##输出1-5
[root@localhost mnt]# awk 'BEGIN{for(i=1;i<=5;i++){print i}}'
1
2
3
4
5
#while循环