第二章 文本处理工具

grep

全称:[Globally search a Regular Expression and Print]

grep

grep -E  = egrep

grep格式

grep 匹配条件 处理文件

grep root passwd

grep -i root passwd

grep -E  " \<root“ passwd     root字符前不能有字符

grep -E  "root\>"  passwd    root字符前不能有字符

grep -数字      显示过滤行以及上面几行和下面几行

grep  -n   显示匹配的行所在的行号

grep -A   显示过滤行以及下面几行

gep -B  显示过滤行以及上面几行

grep -v   反向过滤

 grep字符数量匹配规则

^westos            ##以westos开有
westos$            ##以westos结尾

w....s                   ##w开头s结尾中间4个任意字符
.....s                     ##s结尾前面5个任意字符
*                          ##字符出现任意
?                         ##0到1次
+                         ##1次到任意次
{n}                       ##n次

{m,n}                    ##m到n次

 {0,n}                   ##0-n次
{,n}                       ##0-n次
{m,}                      ##最少m次
(ea){2,}                ##lee字符串出现2次

 

 练习脚本:
请显示系统中能被su命令切换的用户名称

 [root@westoslinux112 mnt]# vim su.sh
[root@westoslinux112 mnt]# sh su.sh
root
westos
[root@westoslinux112 mnt]# cat su.sh
grep -E "bs$|bash$" /etc/passwd | cut -d : -f 1

sed

命令格式

sed 参数 命令 处理对象
sed 参数 处理对象 -f 处理规则文件

 对字符的处理

p                                                     ##显示
sed -n 5p westos                        ##显示第五行
sed -n 3,5p westos                     ##显示3到5行
sed -ne "3p;5p westos               ##显示3行和5行
sed -ne 1,5p westos                      ##1-5行
sed -ne '5,$p' westos                    ##5到最后以行
sed -n '/^#/p' fstab                         ##显示以#开头的行

 d                                             ##删除
sed   5d westos                     ##删除第五行
sed   '/^#/d'  fstab                  ##把#开头的行删除
sed   '/^UUID/!d' fstab          #除了UUID以外的行都删除
sed    -e '5,$d' westos           ##删除5到最后一行

 a                                                           ##添加
sed -e '$a hello world' fstab               默认给fstab文件最后一行添加hello world
sed -e '$a hello\nworld' fstab             给 hello后面添加nworld
sed -e '/^#/a hello world' fstab            给以#开头的行后面添加hello world

 c                  ##替换
sed -e '/^#/c hello world' fstab     将以#开头的行替换为hello world
sed '5chello world' westos            将westos的第五行替换成world中的内容

w                   ##把符合的行写到指定文件中
sed '/^UUID/w westofile' fstab         ##把fstab中UUID开头的行写入westosfile中

i                           ##插入
sed '5ihello westos'      ##整合文件westos    将hello插入到westos的第五行
r                            ##整合文件
sed '5r haha' westos                  将haha整合到westos的第五行

 

 sed 字符替换

sed 's/sbin/westos/g' passwd
sed '1,5s/sbin/westos/g' passwd
sed '1,5s/sbin/westos' passwd                      
sed '2s/sbin/westos/g;5s/sbin/westos/g' passwd     将第二行的/sbin替换成westos并且将第五行的/nologin替换成lee
sed 's/sbin/westos/g;s/nologin/hello/g' passwd     将/sbin替换成westos /nologin替换成lee
sed '/lp/,/halt/s/sbin/westos/g' passwd    将lp到halt列/sbin替换成westos
sed 's@/@###@g' passwd    将passwd 文件中的/替换成###
sed 's@/@###@g' passwd -i westos     

vim rule
1 s/sbin/westos/g
2 s/nologin/lee/g
sed -f rule passwd   执行rule的命令

 练习及脚本
Apache_port.sh
此脚本接入数字
http的端口就改为此数字
假设selinux为关闭状态
例如:
sh Apache_port.sh
ERROR: Pleaase input port number following script !!
sh Apache_port.sh 8080
apache的端口会被修改为8080

[root@westoslinux112 mnt]# vim Apache_port.sh
[root@westoslinux112 mnt]# sh Apache_port.sh 80
80 is in userd !!
[root@westoslinux112 mnt]# cat Apache_port.sh
#!/bin/bash
[ -z "$*" ] && {
  echo "Pleaase input port number following script !!"
  exit
}

 rpm -q httpd &> /dev/null ||{
   echo "apache is not installed !! "
   exit
 }

 netstat -antlupe | grep -E "\<$*\>"  &> /dev/null && {
   echo "$* is in userd !!"
   exit
 }

 sed "/^Listen/cListen $*" -i /etc/httpd/conf/httpd.conf
 systemctl restart httpd

awk

[root@westoslinux112 mnt]# awk -F : 'BEGIN{START=0}{print START}END{print "START"}' passwd   显示出START并且显示"START"
0                                                                                不加“”表示一个变量  加上“”表示一串字符    
0
0
0
0
0
0
0
0
0
0
0
0
START
[root@westoslinux112 mnt]# awk -F : 'BEGIN{START=0}{print NF}END{print "START"}' passwd    显示列数并显示START
7
7
7
7
7
7
7
7

START
[root@westoslinux112 mnt]# awk -F : 'BEGIN{N=0}{N++}END{print N}' passwd   每执行一次只要条件符合就加1,不符合就不加
48
[root@westoslinux112 mnt]# wc -l passwd
48 passwd
[root@westoslinux112 mnt]# awk -F : '/sbin/{print $0}' passwd    显示出全文中/sbin的行
[root@westoslinux112 mnt]# awk -F : '!/sbin/{print $0}' passwd  显示出全文中不是/sbin的行
[root@westoslinux112 mnt]#awk -F : '!/sbin|test/{print $0}' passwd     显示出全文中不是/sbin并且不是test的行
[root@westoslinux112 mnt]# awk -F : '!/sbin/&&!/test/{print $0}' passwd   显示出全文中不是/sbin并且是test的行
[root@westoslinux112 mnt]# awk -F : '/sbin/||/test/{print $0}' passwd   显示出全文中是/sbin或者是test的行
[root@westoslinux112 mnt]# awk -F : '/sbin|test/{print $0}' passwd    显示出全文中是/sbin或者是test的行
[root@westoslinux112 mnt]# awk -F : '/sbin/&&/test/{print $0}' passwd   显示出全文中是/sbin并且是test的行
[root@westoslinux112 mnt]# awk -F : '/sbin/&&/test/{print $1}' passwd
sbin
[root@westoslinux112 mnt]# awk -F : '/sbin/&&/test/{print $2}' passwd  显示第2行
test
[root@westoslinux112 mnt]# awk -F : '/sbin/&&/test/{print $1,$2}' passwd
sbin test
[root@westoslinux112 mnt]# awk -F : '/sbin/&&/test/{print $1":"$2}' passwd  显示1到2行
sbin:test
[root@westoslinux112 mnt]# awk -F : '!/nologin/{print $0}' passwd   
[root@westoslinux112 mnt]# awk -F : '$7~/nologin/{print $0}' passwd
root@westoslinux112 mnt]# awk -F : '$7!~/nologin/{print $0}' passwd    显示全文中第七列不是/nologin的行

课后练习:
统计在系统中能su切换的并且用户加目录不在/home下的用户数量

[root@westoslinux112 mnt]# vim home_check.sh
[root@westoslinux112 mnt]# sh home_check.sh
1
[root@westoslinux112 mnt]# cat home_check.sh
#!/bin/bash
awk -F : 'BEGIN{N=0}$6!~/^\/home/&&/bash$|sh$/{N++}END{print N}' /etc/passwd

猜你喜欢

转载自blog.csdn.net/gk12050802/article/details/121397322