频道栏目
首页 > 资讯 > Linux > 正文

Linux文本处理三剑客grep、sed、awk

18-06-25        来源:[db:作者]  
收藏   我要投稿

一、全局搜索命令grep

Global search regular expression and print out the line 全面搜索研究正则表达式并显示出来
grep 命令是一种强大的文本搜索工具 , 根据用户指定的“模式”对目标文本进行匹配检查 ,
打印匹配到的行由正则表达式或者字符及基本文本字符所编写的过滤条件
1.grep的格式
grep 匹配条件 处理文件
-i 忽略大小写 -E 扩展(相当于egrep) -v 反向过滤 把符合的行屏蔽

示例:

grep root passwd              ##查找包含root的关键词的行
grep ^root passwd             ##查找passwd中以root开头的行
grep root$ passwd             ##查找passwd中以root结尾的行
grep -i root passwd           ##忽略大小写查找root所在的行
grep -E "^root|root$" passwd  ##过滤以root开头或者以root结尾的内容 (-E扩展) (|表示在文本正则表达式中表示逻辑或)
[root@localhost mnt]# cp  /etc/passwd  /mnt/
[root@localhost mnt]# grep root passwd  ##过滤passwd中含有root的项目
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

这里写图片描述

[root@localhost mnt]# vim  passwd   ##添加后三行
test:test:root
test:root:test
ROOT:test:test
[root@localhost mnt]# grep ^root passwd  ##过滤以root开头的内容
root:x:0:0:root:/root:/bin/bash
[root@localhost mnt]# grep root$ passwd  ##过滤以root结尾的内容
test:test:root
[root@localhost mnt]# grep -i  ^root passwd   ##过滤以root开头的内容且(-i)忽略大小写
root:x:0:0:root:/root:/bin/bash
ROOT:test:test
[root@localhost mnt]# grep -E "^root|root$" passwd  ##过滤以root开头或者以root结尾的内容 (-E扩展) (|表示在文本正则表达式中表示逻辑或)
root:x:0:0:root:/root:/bin/bash
test:test:root
[root@localhost mnt]# egrep  "^root|root$" passwd ##grep -E 和egrep 有相同的效果
root:x:0:0:root:/root:/bin/bash
test:test:root

这里写图片描述

[root@localhost mnt]# grep -E -v "^root|root$" passwd ##找出不是以root开头和结尾的项

这里写图片描述

[root@localhost mnt]# grep  -i  root  passwd |grep  -v -i -E  "^root|root$" ##找出root在中间的项
operator:x:11:0:operator:/root:/sbin/nologin
test:root:test
[root@localhost mnt]# grep -v -i -E "^root|root$" passwd | grep root   ##找出root在中间的项
operator:x:11:0:operator:/root:/sbin/nologin
test:root:test

这里写图片描述
2.grep中的正则表达式
grep 中字符的匹配次数设定

符号 涵义
* 字符出现 [0- 任意次]
\? 字符出现 [0-1 次]
+ 字符出现 [1- 任意次]
{n} 字符出现 [n 次 ]
{m,n} 字符出现 [ 最少出现 m 次,最多出现 n 次 ]
{0,n} 字符出现 [0-n 次 ]
{m,} 字符出现 [ 至少 m 次 ]
(xy){n}xy 关键字出现 [n 次 ]
.* 关键字之间匹配任意字符
[root@localhost mnt]# grep -E 'r..t' test  ##匹配rt之间只有两个字符的项
[root@localhost mnt]# grep -E 'r...t' test ##匹配rt之间只有三个字符的项
root@localhost mnt]# grep  -E 'r....t' test ##匹配rt之间只有四个字符的项
[root@localhost mnt]# grep -E 'r*t' test  ##匹配rt之间任意个字符的项
[root@localhost mnt]# grep  -E 'ro*t' test  ##匹配rt之间有任意多个o的项
[root@localhost mnt]# grep -E 'ro?t' test ##匹配rt之间0-1此o的项

这里写图片描述
这里写图片描述

[root@localhost mnt]# grep -E 'ro{1,}t'  test  ##1到任意多次o
[root@localhost mnt]# grep -E 'ro{1,3}t'  test  ##1到3次o
[root@localhost mnt]# grep -E 'ro{3,}t'  test  ##3到任意多次o

这里写图片描述
grep 中字符的匹配位置设定

[root@localhost mnt]# grep -E "r....\>" test ##以r开头且后匹配任意4个字符
[root@localhost mnt]# grep -E "\<....t" test ##以t结尾前任意匹配4个字符
[root@localhost mnt]# grep -E "\<..o..\>" test ##o的前后任意匹配两个字符

这里写图片描述
3.脚本实验
编写脚本找出可以登录系统的用户

[root@localhost mnt]# vim show_loginuser.sh
#!/bin/bash
SHELL=$(echo `grep -v nologin  /etc/shells`|sed  's/ /|/g')
grep -E "$SHELL" /etc/passwd  |cut -d : -f 1

执行脚本:

[root@localhost mnt]# sh show_loginuser.sh 
root
student
[root@localhost mnt]# useradd -s /bin/tcsh  user1  ##新建用户user
[root@localhost mnt]# sh show_loginuser.sh         ##使用脚本可以看到user可以登录
root
student
user1

显示ip的脚本ip_show.sh

[root@localhost mnt]# vim  ip_show.sh
#!/bin/bash
ifconfig  eth1 |grep -E "inet\>" |cut -d " " -f 10
[root@localhost mnt]# chmod +x ip_show.sh 

执行脚本:

[root@localhost mnt]# /mnt/ip_show.sh 
172.25.254.105

二、sed行编辑器

stream editor 用来操作纯 ASCII 码的文本

处理时 , 把当 前处理的行存储在临时缓冲区中 , 称为“模式空间” (pattern space) 可以指定仅仅处理哪些行
sed 符合模式条件的处理 不符合条件的不予处理处理完成之后把缓冲区的内容送往屏幕
接着处理下一行 , 这样不断重复 , 直到文件末尾
1.sed命令格式

sed 参数 命令 file     
sed 参数 -f 脚本 file

sed 对字符的处理
p 显示 d 删除 a 添加 c 替换 w 写入 i 插入

2.p 模式操作
-n:显示输出结果

[root@localhost mnt]# sed  -n '/#/p' fstab   ##显示由#的行
[root@localhost mnt]# sed  -n '/^#/!p' fstab ##显示不以#开头的行
[root@localhost mnt]# sed  -n '/UUID/P ' fstab ##显示有UUID的行

这里写图片描述

[root@localhost mnt]#  cat -n fstab | sed -n '5p'            ##显示fstab中第5行
[root@localhost mnt]# cat -n fstab | sed -n '3,5p'          ##显示fstab中第3-5行
[root@localhost mnt]#  cat -n fstab | sed -n '3,5!p'         ##显示fstab中除第3-5行外所有的行
[root@localhost mnt]# cat -n fstab | sed -ne '3p' -ne '5p'  ##显示fstab中第3、5行
[root@localhost mnt]#  cat -n fstab | sed -ne '3p;5p;8p'     ##显示fstab中第3、5、8行

这里写图片描述

编写脚本,使用userfile和passfile创建用户

[root@localhost mnt]# vim  create_user.sh 
#!/bin/bash
MAX_LINE=`wc -l $1 |cut -d " " -f 1`
for LINE_NUM in `seq 1 $MAX_LINE`
do
        USERNAME=`sed -n "${LINE_NUM}p"  $1`
        PASSWORD=`sed -n "${LINE_NUM}p"  $2`
        useradd $USERNAME
        echo $PASSWORD|passwd  --stdin  $USERNAME
done

脚本的执行:

[root@localhost mnt]# sh create_user.sh  userfile passfile 
passwd: all authentication tokens updated successfully.
Changing password for user user1.
passwd: all authentication tokens updated successfully.
Changing password for user user2.
passwd: all authentication tokens updated successfully.
Changing password for user user3.
passwd: all authentication tokens updated successfully.

3.d模式操作

[root@localhost mnt]# sed  '/^#/d' fstab  ##删除以#开头的项并显示
[root@localhost mnt]# sed  '/1,4/d' fstab ##删除fstab中第1到4行并显示其他行
[root@localhost mnt]# sed  '/^UUID/d' fstab ##删除以UUID开头的行并显示
[root@localhost mnt]# sed  '/^UUID/!d' fstab ##除以UUID开头的行,其余行全部删除
[root@localhost mnt]# cat -n fstab |sed '3d;5d;8d'##删除3,5,8行

这里写图片描述

[root@localhost mnt]# sed -e '/^$/d' fstab ##删除空行
[root@localhost mnt]# sed -e '/^$/d;/^#/d' fstab ##删除#开头和空行

这里写图片描述
4.a模式操作

[root@localhost mnt]# vim  westos
hello
[root@localhost mnt]# sed  '/hello/aworld' westos ##在hello后添加world
hello
world
[root@localhost mnt]# sed  's/hello/hello world/g' westos ##用hello world替换hell
hello world
[root@localhost mnt]# sed '/hello/aworldwestos' westos 
hello
worldwestos
[root@localhost mnt]# sed '/hello/aworld\nwestos' westos ##换行操作
hello
world
westos

5.i模式操作

[root@localhost mnt]# sed '/hello/iworld\nwestos' westos  ##在hello之前插入world westos
world
westos
hello
[root@localhost mnt]# cp  /etc/passwd .
[root@localhost mnt]# vim  passwd ##删除一些内容
[root@localhost mnt]# sed 's/nologin/westos/g'  passwd ##把nologin更改为westos   (g为全局更改)

这里写图片描述

[root@localhost mnt]# sed '3,5s/nologin/westos/g' passwd ##把3到5行的nologin更改为westos

这里写图片描述

[root@localhost mnt]# sed '/adm/,/sync/s/nologin/westos/g' passwd ##把/adm/到/sync/的nologin更改为westos

这里写图片描述

[root@localhost mnt]# sed -e '/adm/,/sync/s/nologin/westos/g;s/sbin/lee/g' passwd 的nologin更改为westos;sbin改为lee

这里写图片描述

[root@localhost mnt]# vim file  ##更改策略文件
s/sbin/westos/g
s/nologin/linux/g
[root@localhost mnt]# sed -f file passwd   ##执行策略文件,不会改变passwd
[root@localhost mnt]# sed -f file  -i passwd ##作为passwd的输入,passwd改变

编写脚本,安装apache并且修改其端口为8080:

[root@localhost mnt]# vim  install_apache.sh 
#!/bin/bash
yum install httpd -y &> /dev/null
sed "/^Listen/cListen $1" -i /etc/httpd/conf/httpd.conf
echo -e "\033[32mthe  listen is changed;\033[0m"
systemctl restart httpd

执行脚本

[root@localhost mnt]# sh  install_apache.sh 8080
the  listen is changed;
[root@localhost mnt]# netstat -antlpe |grep http
tcp6       0      0 :::8080                 :::*                    LISTEN      0          92961      6544/httpd   

三、报告生成器awk

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

1.awk命令

[root@localhost mnt]#awk -F ":"  '{print $1}' passwd ##以:为分隔符打印第一列
[root@localhost mnt]# awk -F ":"  'BEGIN{print "NAME"}{print $1}END{print "END"}' passwd##以:为分隔符打印第一列
[root@localhost mnt]# awk -F ":"  'BEGIN{print "NAME"}{print NR;print }END{print "END"}' passwd##以:为分隔符打印行数和每一行
[root@localhost mnt]# awk  '/bash$/{print }' passwd ##打印以bash为结尾的行数
[root@localhost mnt]# awk  -F  ':' '/bash$/{print $1}' passwd   ##打印以bash为结尾的第一列

这里写图片描述
这里写图片描述
其他截图效果不再赘述,望读者自行演示

[root@localhost mnt]# awk -F : 'BEGIN{N=O}/bash$/{N++}END{print N}' passwd  ##以bash结尾的行数
4
[root@localhost mnt]# awk -F ":"  '{print NR,NF}' passwd ##打印第NR行,总共NF列

这里写图片描述

[root@localhost mnt]# awk '/^ro/{print }' passwd  ##打印以root开头的行
root:x:0:0:root:/root:/bin/bash
[root@localhost mnt]# awk '/^[a-d]/{print }' passwd ##打印以a到d开头的行

这里写图片描述

[root@localhost mnt]# awk -F : '/^a|nologin$/{print $1,$7}' passwd ##打印以a开头或者以nologin结尾的第一列和第七列
[root@localhost mnt]#awk -F ":" '$5~/^a/{print }' passwd  ##打印第五列是以a开头的行
adm:x:3:4:adm:/var/adm:/sbin/nologin
[root@localhost mnt]# awk -F ":" '$7!~/bash$/{print }' passwd ##打印第七列不是以bash结尾的行

这里写图片描述
这里写图片描述
2.awk应用
找出可以登录系统的用户并统计个数

[root@localhost mnt]# awk -F ":" 'BEGIN{N=0}/bash$/{N++}END{print N}' /etc/passwd
4

找出可登录的用户但是家目录不在/home下

[root@localhost mnt]# awk -F ":"  '$6!~/^\/home/&&/bash$/{print $1}' /etc/passwd
root

统计可登录的用户但是家目录不在/home下个数

[root@localhost mnt]# awk -F ":"  'BEGIN{N=0}$6!~/^\/home/&&/bash$/{N++} END{ print N}' /etc/passwd 
1

显示一个文件的行数:

[root@localhost ~]# awk  'BEGIN{N=0}{N++}END{print N}'  /etc/passwd
43

抓取网卡的ip

[root@localhost mnt]# ifconfig eth0 |awk '/inet\>/{print $2}'
172.25.4.105

这里写图片描述

相关TAG标签
上一篇:关于vmware中网络连接的桥接、NAT、主机模式的原理和区别介绍
下一篇:关于oracle登录的问题总结
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站