sed:stream editor :一次处理一行内容,处理时,把当前的行存储在临时缓冲区,处理完后,输送到屏幕
sed [参数] '命令' file
p ##显示
d ##删除
a ##添加
c ##替换
i ##插入
p静默状态下的显示:
1.
[root@localhost mnt]# cat /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]# cat -n /etc/fstab
1
2 #
3 # /etc/fstab
4 # Created by anaconda on Wed May 7 01:22:57 2014
5 #
6 # Accessible filesystems, by reference, are maintained under '/dev/disk'
7 # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
8 #
9 UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 / xfs defaults 1 1
[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
UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /
xfs defaults 1 1
[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 显示2-6行
#
# /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 显示除过2-6行
# 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
d删除模式
[root@localhost mnt]# sed '/^UUID/d' /etc/fstab 删除以UUID开头的行
#
# /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 删除以#开头的行
[root@localhost mnt]# sed '/^$/d' /etc/fstab 删除空格
[root@localhost mnt]# sed '1,4d' /etc/fstab 删除1-4行
#
a:添加模式
[root@localhost mnt]# cat westos
haha
[root@localhost mnt]# sed '/haha/ahello' westos
haha
hello
c:替换
[root@localhost mnt]# sed '/haha/chello world' westos
hello world
i:插入(不改变源文件内容)
[root@localhost mnt]# sed '/haha/iworld\nwestos' westos
world
westos
haha
i (改变源文件内容)
[root@localhost mnt]# sed -i 's/haha/redhat/' westos
[root@localhost mnt]# cat westos
redhat
[root@localhost mnt]# sed -i 's/redhat/westos/g' westos 全局替换
[root@localhost mnt]# cat westos
westos
[root@localhost mnt]#
2.awk报告生成器
1)awk处理机制:根据模式一次从文件中抽取一行文本,对这行文本进行切片(默认使用空白字符作为分隔符)
[root@server mnt]# cat test
this | is | a | file
$1 $2 $3 $4
awk '{print $0}' test ##$0表示输出一整行
awk '{print $1}' test
awk '{print $4}' test
awk '{print $1,$2}' test ##显示两个字段
awk -F ":" '{print $1,$3}' /etc/passwd ##指定分隔符
2)awk常用变量
awk '{print FILENAME,NR}' /etc/passwd ##输出文件名,和当前操作的行号
awk -F: '{print NR,NF}' /etc/passwd ##输出每次处理的行号,以及当前以":"为分隔符的字段个数
总结:awk '{print "第NR行","有NF列"}' /etc/passwd
BEGIN{}:读入第一行文本之前执行的语句,一般用来初始化操作
{}:逐行处理
END{}:处理完最后以行文本后执行,一般用来处理输出结果
awk 'BEGIN { a=34;print a+10 }'
awk -F: 'BEGIN{print "REDHAT"} {print NR;print } END {print "WESTOS"}' passwd ##文件开头加REDHAT,末尾加WESTOS,打印行号和内容
awk -F: '/bash$/{print}' /etc/passwd ##输出以bash结尾的
awk -F: 'NR==3 {print}' /etc/passwd
awk -F: 'NR % 2 == 0 {print}' /etc/passwd ##偶数行
awk -F: 'NR >=3 && NR <=5 {print }' /etc/passwd
awk 'BEGIN{i=0}{i+=NF}END{print i}' linux.txt ##统计文本总字段个数