SHELL文本处理三剑客(sed、grep、awk)

一、grep文本过滤器

1. 基本介绍

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

2. grep中字符的匹配位置设定
grep 匹配条件 处理文件
    ^关键字  #关键字开头的行
    关键字$ #关键字结尾的行
    \<关键字 #不再向前扩展
    关键字\> #不再向后扩展
    \<关键字\> #不再向前后扩展

例如:

[root@sunshine shell]# grep root passwd                    #匹配有root的行
[root@sunshine shell]# grep ^root passwd                   #匹配root开头的行
[root@sunshine shell]# grep root$ passwd                   #匹配root结尾的行          
[root@sunshine shell]# grep -i ^root passwd                #-i忽略大小写
[root@sunshine shell]# grep -i -E "^root|root$"  passwd   #-E识别|符号,"|"表示“或”
[root@sunshine shell]# grep -i -E -v "^root|root$"  passwd #-v反向过滤

这里写图片描述

2. grep中字符的匹配次数设定
* #字符出现0-任意次
\? #字符出现0-1次
+ #字符出现0-任意次
{n} #字符出现n次
{m,n} #字符出现m-n次
{0,n} #字符出现0-n次
{m,} #字符最少出现m次
(xy){n}xy #关键字出现n次
.* #关键字之间匹配任意字符要加-E 

例如:

[root@sunshine shell]# grep -E "r*t" file
[root@sunshine shell]# grep -E "r.*t" file       #匹配rt之间任意字符的行
[root@sunshine shell]# grep -E "r..." file       #匹配r后面有三个字符字符的行
[root@sunshine shell]# grep -E "r...\>" file     #匹配r后面有三个字符字符的行,不向后做扩展
[root@sunshine shell]# grep -E "\<r..." file     #匹配r后面有三个字符字符的行,不向前做扩展
[root@sunshine shell]# grep -E "\<r...\>" file   #匹配r后面有三个字符字符的行,不向前后做扩展
[root@sunshine shell]# grep -E "ro*t" file       #匹配rt之间o出现0-任意次的行
[root@sunshine shell]# grep -E "ro?t" file       #匹配rt之间o出现0-1次的行
[root@sunshine shell]# grep -E "r0{1,}t" file    #匹配rt之间o出现1-任意次的行
[root@sunshine shell]# grep -E "ro{1}t" file     #匹配rt之间o出现1次的行
[root@sunshine shell]# grep -E "ro{0,1}t" file   #匹配rt之间o出现0-1次的行
[root@sunshine shell]# grep -E "(root){1,}" file #匹配root关键子出现1-任意次的行

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

脚本编写实例:
编写脚本检测哪些用户可以登录主机

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

这里写图片描述
脚本内容
这里写图片描述

二、sed行编辑器

1. 基本介绍
1)sed行编辑器

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

2)调用 sed 命令有两种形式:
            sed [options] 'command' file(s)
            sed [options] -f scriptfile file(s)
3)选项
 -n :使用安静(silent)模式。在一般 sed 的用法中,所有来自 STDIN 的数据一般都会被列出到屏幕上。 
 但如果加上 -n 参数后,则只有经过 sed 特殊处理的那一行(或者动作)才会被列出来。
-e :直接在命令行界面上进行 sed 的动作编辑;
-f :直接将 sed 的动作写在一个文件内,-f filename 则可以执行 filename 内的 sed 动作;
-r :sed 的动作支持的是延伸型正则表达式的语法。(默认是基础正则表达式语法)
-i :直接修改读取的文件内容,而不是由屏幕输出。
4)sed 对字符的处理

p 显示
d 删除
a 添加
c 替换
w 写入
i 插入

2.sed 的常见操作
1) p显示

例如:

[root@sunshine shell]# sed -n '/\:/p' fstab        #显示有:的行
[root@sunshine shell]# sed -n '/UUID$/p' fstab     #显示UUID结尾的行
[root@sunshine shell]# sed -n '/^UUID/p' fstab     #显示UUID开头的行
[root@sunshine shell]# sed -n '2,6p' fstab         #显示2到6行
[root@sunshine shell]# sed -n '2,6!p' fstab        #不显示2到6行
[root@sunshine shell]# cat -n fstab | sed -ne '2!p;6!p' | uniq -d #不显示第二行>
和第六行

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

脚本编写实例:
编写脚本建立用户,用户名为userfile里面的内容,密码为passwordfile里面的内容

[root@sunshine shell]# vim user_create.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@sunshine shell]# sh user_create.sh userfile passwordfile 

脚本及文件内容:
这里写图片描述
这里写图片描述
执行结果:
这里写图片描述

2)a添加(默认添加到关键字下面一行)
[root@sunshine shell]# sed -e '/hello/aworld' file
3)i 插入(默认插入到关键字上面一行)
[root@sunshine shell]# sed -e '/hello/iworld' file
4)c 替换
[root@sunshine shell]# sed -e '/hello/cworld' file
5)d 不显示
[root@sunshine shell]# sed -e '/^#/d;/^$/d' fstab

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

6)w 写入
[root@sunshine shell]# sed '/^UUID/w file' fstab                #保存到文件并显示
[root@sunshine shell]# sed -n '/^UUID/w file' fstab             #保存到文件不显示

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

7)sed其他用法
[root@sunshine shell]# sed '/^UUID/= ' fstab                    #显示UUID的行数,结果和内容
[root@sunshine shell]# sed -n -e '/^UUID/p' -e '/^UUID/= ' fstab#显示UUID的行数,只显示结果
[root@sunshine shell]# sed 'G' fstab                            #每行后加空行
[root@sunshine shell]# sed '$!G' fstab                          #每行后加空行,不包括最后一行
[root@sunshine shell]# sed 's/nologin/linux/g' passwd           #替换,g表示全文替换
[root@sunshine shell]# sed -e '/adm/,/sync/s/nologin/linux/g' passwd 
[root@sunshine shell]# sed 's/nologin/linux/g' -i  passwd       #替换并保存
[root@sunshine shell]# sed '=' test | sed 'N; s/\n/ /'  #成行号,并将两行间的换行变成空格

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

三、awk报告生成器

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

1. awk 通过 print 的功能将字段数据列出来
1)方式一
 awk -F : '{print NR " " NF " " $1  }' passwd
     -F :分隔符类型,默认为空格
     NR :第几行
     NF :有几列
     $1 :以“:”为分隔符的第一列数据

这里写图片描述

2)方式二
 awk -F : 'BEGIN{print "NAME"}{print $1}END{print "END"}' passwd
      # BEGIN {print "NAME"} 处理数据前,执行显示"NAME"字符
      # END {print "END"} 处理数据后,执行显示"END"字符

这里写图片描述

2. 一些其他的用法表达
1)
 awk '/bash$/' passwd   # 列出以 bash 结尾的行

这里写图片描述

2)统计一个文件有多少行
awk 'BEGIN{N=0}{N++}END{print N}' /etc/passwd

这里写图片描述

3)对指定行与内容进行显示
 awk -F : '/^ro/{print}' passwd
     # 以:为分隔符,显示以 ro 字符开头的行
 awk -F : '/^[a-d]/{print $1,$6}' passwd
     # 显示以字母 a-d 开头的行的第 1 列,第 6 列
 awk -F : '/^a|nologin$/{print $1,$7}' passwd
     # 显示以 a 开头,以 nologin 结尾的行的第 1 列,第 7 列
 awk -F : '$6~/bin$/{print $1,$6}' passwd
     # 显示从第 6 列开始,以 bin 结尾的列所在行的第 1 列,第 6 列 
 awk -F : '$7!~/nologin$/{print $1,$6}' passwd
     # 显示除了从第 7 列开始以 nologin 结尾的行的第 1 列,第 6 列

猜你喜欢

转载自blog.csdn.net/mys_sunshine/article/details/80753397