Shell 文本处理工具(grep、sed、awk)

一、grep文本过滤命令

全局搜索研究正则表达式并显示出来
grep 命令是一种强大的文本搜索工具,根据用户指定的“模式”对目标文本进行匹配检查,打印匹配到的行,由正则表达式或者字符机基本文本字符所编写的过滤条件

1、grep匹配字符

-E 正则表达式
grep 匹配条件 处理文本
grep root passwd //把passwd含有root的行导出来
grep ^root passwd //找出root开头的行
grep root$ passwd //找出以root结尾的行
grep -i root passwd //-i忽略大小写含有root的行
grep -E “^root|root$” passwd //过滤以root开头或者以root结尾的行,|表示或
grep -E -v “^root|root$” passwd //-v 反向过滤

实验:
这里写图片描述

题目:怎么样把root在中间的行过滤出来
操作如下:

在这里插入图片描述

2、grep中字符的匹配次数设定

grep -E ‘ro*t’ test //搜索含有0-任意o的以t结尾的
grep -E ‘ro{1,}’ test //搜索含有1-任意o的
grep -E ‘ro{1,2}’ test //搜索含有1,2个o的
grep -E ‘ro{,2}’ test //搜索0-2个o的
grep -E ‘ro+t’ test //搜索1-任意o的
grep “ro+t” test //\表示转义
grep -E ‘(root){1,2}’ test //搜索含有1个或2个连续root的
grep -E ‘root’ test //搜索含有root的
grep -E ‘(root){2,}’ test //搜索2个以上root连续的
grep -E ‘r…t’ test //搜索r和t中间有两个字符的
grep -E ‘r…t’ test //搜索r和t中间含有3个字符的
grep -E ‘r?t’ test //字符出现0-一次
grep -E ‘r.*t’ test //搜索r和t中任意字符的
* 字符出现零到任意次
. 关键字之间匹配任意字符
? 字符出现零到一次
+ 字符出现1到任意次
{n,m} 字符至少出现n次,至多出现m次
{,m} 字符出现0到m次
{n,} 字符出现n以上

实验:

[root@node1 mnt]# vim test
[root@node1 mnt]# grep -E 'ro*t' test  //搜索含有0-任意o的以t结尾的
root
rootoroot
rooot
root
rootrootrroot
rootrootrrrootroot
rot
rt
[root@node1 mnt]# grep -E 'ro{1,}' test  //搜索含有1-任意o的
root
rootoroot
rooot
root
rootrootrroot
rootrootrrrootroot
rot
[root@node1 mnt]# grep -E 'ro{1,2}' test  //搜索含有1,2个o的
root
rootoroot
rooot
root
rootrootrroot
rootrootrrrootroot
rot
[root@node1 mnt]# grep -E 'ro{,2}' test   //搜索0-2个o的
root
rootoroot
rooot
root
rootrootrroot
rootrootrrrootroot
rot
rt
rht
rst
[root@node1 mnt]# grep -E 'ro+t' test  //搜索1-任意o的
root
rootoroot
rooot
root
rootrootrroot
rootrootrrrootroot
rot
[root@node1 mnt]# grep -E '(root){1,2}' test  //搜索含有1个或2个连续root的
root
rootoroot
root
rootrootrroot
rootrootrrrootroot
[root@node1 mnt]# grep -E 'root' test    //搜索含有root的
root
rootoroot
root
rootrootrroot
rootrootrrrootroot
[root@node1 mnt]# grep -E '(root){2,}' test   //搜索2个以上root连续的
rootrootrroot
rootrootrrrootroot
[root@node1 mnt]# grep -E 'r..t' test   //搜索r和t中间有两个字符的
root
rootoroot
root
rootrootrroot
rootrootrrrootroot
[root@node1 mnt]# grep -E 'r...t' test   //搜索r和t中间含有3个字符的
rooot
rootrootrroot
rootrootrrrootroot
[root@node1 mnt]# grep -E 'r?t' test   //字符出现0-一次
root
rootoroot
rooot
root
rootrootrroot
rootrootrrrootroot
rot
rt
rht
rst
[root@node1 mnt]# grep -E 'r.*t' test   //搜索r和t中任意字符的
root
rootoroot
rooot
root
rootrootrroot
rootrootrrrootroot
rot
rt
rht
rst

3、grep中字符的匹配位置设定

^关键字 关键词开头
关键字$ 关键结尾
\<关键字 关键字结尾不扩展
\>关键字 关键字开头不扩展
\<关键字>\ 精确匹配关键字

实验:

[root@node1 mnt]# grep ^root passwd  //找出root开头的行
root:x:0:0:root:/root:/bin/bash
root:hello:root
[root@node1 mnt]# grep root$ passwd  //找出以root结尾的行
root:hello:root
hello:root:root
[root@node1 mnt]# grep -E  'r..t\>' test  //后面加\>,防止搜索做拓展搜索
root
rootoroot
root
rootrootrroot
rootrootrrrootroot
[root@node1 mnt]# grep -E  '\<r..t' test  //前面加\<,防止搜索做拓展搜索
root
rootoroot
root
rootrootrroot
rootrootrrrootroot

题目:编写一个脚本,来建立文件userfile里面的用户,对应使用文件passfile里面的密码
操作如下:
[root@node1 mnt]# vim userfile  //编写用户名文件

内容如图:这里写图片描述

[root@node1 mnt]# vim passfile  //编写密码文件

内容如图:这里写图片描述

[root@node1 mnt]# vim create_user.sh  //编写脚本

脚本内容如下:
这里写图片描述
执行如下:
这里写图片描述

二、sed行编辑器

stream editor
用来操作纯ASCLL码的文本
处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space)可以制定仅仅处理的那些行
sed符合模式条件的处理,不符合条件不予以处理
处理完成之后把缓冲区的内容送往屏幕
接着处理下一行,这样不断重复,直到文件末尾

1、p 操作模式

p 表示显示
sed -n ‘/#/p’ fstab 显示文件带#的行
sed -n ‘/#/p’ fstab -i fstab 将显示结果覆盖掉原文件
cat -n fstab | sed -ne ‘3p;5p’ 显示文件的3,5行
cat -n fstab | sed -ne ‘1,5!p’ 不显示文件1到5行
-n 只显示匹配空间的内容,不显示输出
-e 处理多个指令

实验:
这里写图片描述
这里写图片描述
这里写图片描述

2、d 操作模式

d 表示删除
sed ‘1,4d’ fstab //删除文件1、4行
sed -e ‘/^$/d’ fstab //删除空行
sed -e ‘/^$/d;6d’ fstab //删除空行和第六行
sed -e ‘/^#/d’ fstab //删除#开头的行
sed ‘/^UUID/!d’ fstab //除了以UUID开头的行都删除

实验:
这里写图片描述
这里写图片描述

3、a 操作模式

a 表示添加,在关键字的下面
sed ‘/^UUID/a hello’ fstab 在以UUID开头那一行后添加hello行
sed ‘/^UUID/a hello\ntest’ fstab 在以UUID开头的那一行后插入hello行和test行2行

实验:
这里写图片描述

4、c 操作模式

c 表示替换
sed ‘/^UUID/c hello’ fstab 将以UUID开头的行替换成hello行

实验:
这里写图片描述

4、i 操作模式

i 表示插入,在关键字的上面
sed ‘/^UUID/i hello’ fstab 在以UUID开头的那一行前插入hello行
sed ‘/^UUID/i hello\ntest’ fstab 在以UUID开头的那行前插入world行和westos行

实验:
这里写图片描述

5、w 操作模式

w 表示写入
sed ‘/^UUID/w /mnt/test’ fstab \\将文件UUID开头那一行写入/mnt/test
sed -n ‘/^UUID/w /mnt/test’ fstab \\同上,但-n没有输出,只显示处理结果
sed ‘/^UUID/=’ fstab \\将文件fstab以UUID开头的行号输出,=表示加行号
sed ‘1r /mnt/hello’ fstab \\将/mnt/hello文件写入fstab的第一行
sed ‘$r /mnt/hello’ fstab \\将/mnt/hello文件写入fstab的最后一行

实验:
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述

6、其他参数

sed -n -f prctise fstab 对文件执行prctise的策略
/^UUID/p 文件内容
/^UUID/= 文件内容
sed -n -e ‘/^UUID/p’ -ne ‘/^UUID/=’ fstab 同上
sed -n -e ‘/^UUID/p;/^UUID/=’ fstab
sed ‘s/w/W/g’ fstab 将全文w换成W,与sed ‘s@w@W@g’ fstab 等同
sed ‘1,3s/w/W/g’ fstab 将前3行的w换成W
sed ‘/adm/,/sync/s/nologin/westos/g’ passwd 替换adm到sync中间的nologin为westos
sed ‘G’ fstab G加空行,在每一行后面加空行
sed ‘$!G’ fstab 在最后一行不加空行
sed ‘!G’ fstab 不加空行
sed ‘=’ fstab 显示行号

实验:
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述

题目:编写脚本安装apache并修改端口为666
操作如下:
[root@node1 mnt]# vim install_apache.sh

文件内容如下:
这里写图片描述
执行脚本:

[root@node1 mnt]# sh install_apache.sh 666
the Listen is changed
[root@node1 mnt]# netstat -antlupe | grep 666  //查看端口是否改变
tcp6       0      0 :::666                  :::*                    LISTEN      0          57908      4767/httpd  

三、awk报告生成器

awk处理机制:
awk会逐行处理文本,支持在处理第一行之间做一些准备工作,以及在处理完最后一行做一些总结性质的工作,在命令格式上分别体现如下:
BEGIN{}: 读入第一行文本之前执行,一般用来初始化操作
{}: 逐行处理,逐行读入文本执行相应的处理,是最常见的编辑指令块
END{}: 处理完最后一行文本的执行,一般用来输出处理结果

awk -F “:” ‘BEGIN{print “NAME”}{print $1}’ /etc/passwd
以:为分隔符,处理前打印NAME,打印第一列
awk -F “:” ‘BEGIN{print “NAME” }{print $1}END{print NR}’ /etc/passwd
以:为分隔符,处理打印前NAME,打印第一列,处理后打印行数
awk -F “:” ‘/bash$/’ /etc/passwd
以:为分隔符打印以bash结尾的行
awk -F “:” ‘/bash$/{print $5}’ /etc/passwd
以:为分隔符,打印以bash结尾的第5列
awk -F “:” ‘NR==3’ /etc/passwd
以:为分隔符,打印第三行
awk -F “:” ‘BEGIN{print “NAME”}NR<=3&&NR>=2{print $1}’ /etc/passwd
以:为分隔符,处理前打印NAME,打印2-3列的第一个字符

实验:

[root@node1 mnt]# cat passwd  //查看文件内容
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
user:x:1003:1003::/home/user:/bin/bash
root:hello:root
hello:root:hello
hello:root:root
ROOT:hello:ROOT
[root@node1 mnt]# awk -F ":" 'BEGIN{print }{print $1}' passwd  //以:为分隔符,处理前打印NAME,打印第一列 

root
bin
daemon
user
root
hello
hello
ROOT
[root@node1 mnt]# awk -F ":" 'BEGIN{print }{print $1}END{print NR}' passwd  
//以:为分隔符,处理打印前NAME,打印第一列,处理后打印行数 

root
bin
daemon
user
root
hello
hello
ROOT
8
[root@node1 mnt]# awk -F ":" '/bash$/' passwd  //以:为分隔符打印以bash结尾的行
root:x:0:0:root:/root:/bin/bash
user:x:1003:1003::/home/user:/bin/bash
[root@node1 mnt]# awk -F ":" '/bash$/{print $5}' passwd
root

[root@node1 mnt]# awk -F ":" 'NR==3' /etc/passwd  //以:为分隔符,打印第三行 
daemon:x:2:2:daemon:/sbin:/sbin/nologin
[root@node1 mnt]# awk -F ":" 'BEGIN{print }NR<=3&&NR>=2{print $1}' passwd
以:为分隔符,处理前打印NAME,打印2-3行的第一个字符
bin
daemon
题目:找出可以登录的用户数
操作如下:

这里写图片描述

题目:找出可以登录的用户家目录不在home底下
操作如下:

这里写图片描述

题目:输出本机ip
操作如下:

这里写图片描述

题目:显示文件/etc/passwd的行数
操作如下:

这里写图片描述

猜你喜欢

转载自blog.csdn.net/Ying_smile/article/details/80760593