shell 脚本 的 基本知识(II)

shell中的文本处理

1、grep--文本过滤命令

全面搜索研究正则表达式并显示出来;

grep命令是一种强大的文本搜索工具,根据用户指定的“模式”对目标文本进行匹配检查,打印匹配到的行;

由正则表达式或者字符及基本文件字符所编写的过滤条件

[root@localhost ~]# cp /etc/passwd /mnt/
[root@localhost ~]# cd /mnt/
[root@localhost mnt]# ls
passwd
[root@localhost mnt]# grep root passwd   ##把/passwd 下含有的root行找出来
[root@localhost mnt]# grep ^root passwd  ##^关键字 显示关键字在开头的的内容
[root@localhost mnt]# grep root$ passwd  ##关键字$ 显示关键字在末尾的内容
[root@localhost mnt]# grep -i  root passwd  
[root@localhost mnt]# grep -E "root|ROOT" passwd  ##扩展正则表达式,显示passwd文本里含有root或者ROOT的内容,这里|也有通道符的意思,所以属于扩展正则表达式,要用grep -E 或 egrep

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

[root@localhost mnt]# grep -E  -v "root|ROOT" passwd ##关键字 文本 -v 显示除了关键字的其他内容
[root@localhost mnt]# grep -ni  root passwd  ##-ni 表示不区分匹配条件的大小写并且显示行数

这里写图片描述

显示root在中间的内容,这里用两条命令结合实现:
[root@localhost mnt]# grep -i root passwd | grep -v -i -E "^root|root$"
operator:x:11:0:operator:/root:/sbin/nologin
[root@localhost mnt]# grep -v -i -E "^root|root$" passwd | grep root
operator:x:11:0:operator:/root:/sbin/nologin
[root@localhost mnt]# 

这里写图片描述

grep  x..y     匹配xy中间有两个字符的内容
      x....y   有几个点点就有几个字符
[root@localhost mnt]# grep 'r..t' test
[root@localhost mnt]# grep 'r*t' test  ####开头为r结尾为t的中间字符出现[0-任意次]

这里写图片描述

[root@localhost mnt]# grep 'r\?t' test  ##开头为r结尾为t的中间字符出现[]
[root@localhost mnt]# grep 'r\+t' test  ##开头为r结尾为t的中间字符出现[]

这里写图片描述

[root@localhost mnt]# grep 'ro*t' test
[root@localhost mnt]# grep 'r*t' test

这里写图片描述

[root@localhost mnt]# grep -E 'ro?t' test  ##开头为r结尾为t的中间字符出现[o-1次]
[root@localhost mnt]# grep -E 'ro{1,}t' test  ##开头为r结尾为t的中间字符出现 []
[root@localhost mnt]# grep -E '(root){2,}' test  ##出现两次或者两次以上root 

这里写图片描述

[root@localhost mnt]# grep -E "r...\>" test  ## 关键字\>  后面有堵墙 
[root@localhost mnt]# grep -E "...t" test
[root@localhost mnt]# grep -E "\<...t" test   ## \<关键字 前面有堵墙 前面什么都不能有

这里写图片描述

<一、测试:从ifconfig eth0里取出ip>

这里写图片描述

<二、测试:列出可登陆用户>

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

2、sed--行编辑器 stream editor

用来操作出ASCLL码的文本;

处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(Pattern space)可以指定仅仅处理哪些行为;

符合模式条件的处理,不符合模式条件的不予处理,处理完成之后八缓冲区的内容送往屏幕,接着处理下一行,这样不断重复,直到文件末尾。

1)在p--显示模式下操作

[root@localhost mnt]# sed -n '/^#/p' fstab     ##显示fstab以#号开头的行

这里写图片描述

[root@localhost mnt]# sed -n '/^#/!p' fstab   ##显示除了#开头的行

这里写图片描述

[root@localhost mnt]# cat -n fstab |sed -n '2,6p'   ##显示26行之间的行数
[root@localhost mnt]# cat -n fstab |sed -n '2p'    ##显示第二行
[root@localhost mnt]# cat -n fstab |sed -n '6p'    ##显示第六行
[root@localhost mnt]# cat -n fstab |sed -n -e '2p' -e '6p'  ##显示第二行和第六行
[root@localhost mnt]# cat -n fstab |sed -n '2p;6p'  ##显示第二行和第六行

这里写图片描述

[root@localhost mnt]# cat -n fstab |sed -ne '2!p;6!p' | uniq -d   ##显示除了第二行和第六行之外的行数

这里写图片描述

<三、测试:显示出1-10的数字 和 建立用户>

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

2)d--删除模式下的操作

[root@localhost mnt]# cat -n fstab |sed -e '2d;6d'   ##删除2到6行之间的行数
[root@localhost mnt]# cat -n fstab |sed -e '2,6d'    ##删除第二行和第六行
[root@localhost mnt]# sed -e '/^#/d' fstab    ##删除#开头的行
[root@localhost mnt]# sed -e '/^$/d' fstab    ##删除文本空白行
[root@localhost mnt]# sed -e '/^$/d;/^#/d' fstab   ##删除空白行和#开头的行

这里写图片描述

[root@localhost mnt]# sed -e '/UUID/d' fstab   ##删除关键字UUID的行数
[root@localhost mnt]# sed -e '/UUID/!d' fstab   ##删除除了关键字UUID的行数

这里写图片描述

3)a--添加模式下的操作

a 添加但不保存(\n表示换行),-i保存但是不显示

[root@localhost mnt]# sed '/hello/a\world' westos  
[root@localhost mnt]# sed 's/hello/hello world/g' westos
[root@localhost mnt]# sed '/hello/aworld\nwestos' westos
[root@localhost mnt]# sed '/hello/a\world' -i westos

这里写图片描述

4)c--替换模式下的操作

[root@localhost mnt]# sed '/hello/chello world' westos
[root@localhost mnt]# sed '/hello/chello\nworld' westos

这里写图片描述

5)w--写入模式下的操作

[root@localhost mnt]# sed -n '/bash$/wfile' passwd  ##将passwd的内容写进file
[root@localhost mnt]# sed -n '/hello/wfile' westos  ##将westos的内容写进file

这里写图片描述

[root@localhost mnt]# sed '$r westos' fstab  ##将westos的内容写入fstab最后一行
[root@localhost mnt]# sed '1r westos' fstab  ##将westos的内容写入fstab第一行

这里写图片描述

6)sed 其他用法

[root@localhost mnt]# sed 's/nologin/westos/g' passwd ##将nologin转换成westos  

这里写图片描述

[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/jane/g' passwd       ##将关键字adm开始到sync之间的nologin转换成westos;同时将sbin转换成jane    -e 可以指定多个条件

这里写图片描述

[root@localhost mnt]# sed -e 's@/@@g;s/://g' passwd  ##将/替换成空白

这里写图片描述

[root@localhost mnt]# sed -n '/^UUID/=' fstab
[root@localhost mnt]# sed -n -e '/^UUID/p;/^UUID/=' fstab
             ##显示满足条件的关键字在第几行 “=”

这里写图片描述

[root@localhost mnt]# sed -f rule fstab       ##-f rule指定规则;可以在rule里写入多条规则,-f执行

这里写图片描述

[root@localhost mnt]# sed 'G' passwd    ##’G‘  起到加空行的作用

这里写图片描述

[root@localhost mnt]# sed '$!G' passwd    ##$!G除了最后一行不加空行,其他都加

这里写图片描述

[root@localhost mnt]# sed '=' passwd     ##‘=’ 加行数

这里写图片描述

[root@localhost mnt]# sed '=' passwd | sed 'N; s/\n//g'   ##在上面的条件下去除空行,在前面添加行数

这里写图片描述

综合测试grep和sed:

grep ich /usr/share/mime/packages/freedesktop.org.xml| sed ‘s/^ //g’将文件里含有ich的把全部空格删除掉*

这里写图片描述

<四、测试:更改阿帕奇端口>

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

3、awk--报告生成器

awk处理机制:awk会逐行处理文本,支持在处理的第一行之前做一些准备工作,以及在处理完的最后一行做一些总结性质的工作。

在命令格式下分别体现为:

BEGIN{}:读入第一行文本之前执行,一般用来初始化操作;

{}:逐行处理,逐行读入文本执行相应的处理,最常见的时编辑指令快;

END{}:处理完最后一行文本之后执行,一般用来输出处理结果。

awk
-F 指定分隔符
{print $1}输出第一列的内容
BEGIN{}起始条件
END{}结束条件

[root@localhost mnt]# awk -F ":" '{print $1}' passwd     ##以:为分隔符,打印第1列
[root@localhost mnt]# awk -F ":" 'BEGIN{print "NAME"}{print $1}' passwd    ## ##以:为分隔符,处理前打印 NAME ,打印第1

这里写图片描述

[root@localhost mnt]# awk -F ":" 'BEGIN{print "name"}{print $1}' passwd    ## ##以:为分隔符,处理前打印 name ,打印第1列
[root@localhost mnt]# awk -F ":" 'BEGIN{print "NAME"}{print $1}END{print "END"}' passwd   ##以:为分隔符,处理前打印 NAME ,打印第1列,处理后打印 END

这里写图片描述

[root@localhost mnt]# awk -F ":" 'BEGIN{print "NAME"}{print NR}END{print "END"}' passwd    ##以:为分隔符,处理前打印 NAME ,打印第1列,处理后打印NR行数(NF列)
[root@localhost mnt]# awk -F ":" 'BEGIN{print "NAME"}{print NR$1}END{print "END"}' passwd 

这里写图片描述

[root@localhost mnt]# awk -F ":" 'BEGIN{print "NAME"}{print }END{print "END"}' passwd   ##空格表示所有内容

这里写图片描述

[root@localhost mnt]# awk '/bash$/{print}' passwd        ##打印以bash结尾
[root@localhost mnt]# awk -F ":" '/bash$/{print $1}' passwd     ##以:为分隔符,打印以bash结尾行的第1

这里写图片描述

<五、测试:可以登陆系统的个数>

这里写图片描述

[root@localhost mnt]# awk '/^ro/{print}' passwd    ##打印以ro开头
[root@localhost mnt]# awk '/^[a-d]/{print}' passwd   ##打印以a,b,c,d开头
[root@localhost mnt]# awk '/^[^a-d]/{print}' passwd   ##打印除了以a,b,c,d开头
[root@localhost mnt]# awk '/^r/&&/bash$/{print}' passwd    ##打印以r开头并且bash结尾
[root@localhost mnt]# awk '/^r/||/bash$/{print}' passwd   ## 打印以r开头并且bash结尾

这里写图片描述

[root@localhost mnt]# awk -F  ":" '$5~/^a/{print}' passwd       ##打印第五列以a开头
[root@localhost mnt]# awk -F  ":" '$1~/^r/{print}' passwd       ##打印第一列以r开头
[root@localhost mnt]# awk -F  ":" '$1!~/^r/{print}' passwd      ##打印除了第一列以r开头
[root@localhost mnt]# awk -F  ":" '$7!~/bash$/{print}' passwd      ##打印除了第七列以bash结尾

这里写图片描述

[root@localhost mnt]# awk -F  ":" '$7!~/bash$/{print}' passwd      ##打印除了第七列以bash结尾
[root@localhost mnt]# awk -F  ":" '{print NR,$0}' passwd      ##打印文件所有的并显示行数
[root@localhost mnt]# awk -F  ":" '{print NF,$0}' passwd     ##打印文件所有的并显示列数

这里写图片描述

<六、测试:打印能登陆系统且家目录不是/home的用户名称>

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

这里写图片描述
<七、测试:打印能登陆系统且家目录不是/home的用户个数>

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

这里写图片描述
<八、测试:打印设备eth0的IP>

[root@localhost mnt]# awk -F = '/IPADDR/{print $2}' /etc/sysconfig/network-scripts/ifcfg-eth0
172.25.254.129
或者

[root@localhost mnt]# ifconfig eth0 | awk -F " " '/inet\ /{print $2}'
172.25.254.129

这里写图片描述
<九、测试:打印如何查看一个文件多少行>

[root@localhost mnt]# awk 'BEGIN{n=0}{n++}END{print n}' /etc/passwd
44

这里写图片描述

猜你喜欢

转载自blog.csdn.net/janenancy/article/details/80751006