1.grep命令

Global search regular expression and print out theline
全面搜索研究正则表达式并显示出来
grep命令是一种强大的文本搜索工具根据用户指定的 “模式”
对目标文本进行比配检查,打印匹配到的行
由正则表达式或者字符及基本文本字符所编写的过滤条件
1)grep中字符的匹配位置设定
grep 匹配条件 处理文件
^关键字 #关键字开头的行
关键字$ #关键字结尾的行
\<关键字 #不再向前扩展
关键字> #不再向后扩展
\<关键字> #不再向前后扩展
例如:

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

grep正则 匹配次数_linux


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

* #字符出现0-任意次

\? #字符出现0-1次

+ #字符出现0-任意次

{n} #字符出现n次

{m,n} #字符出现m-n次

{0,n} #字符出现0-n次

{m,} #字符最少出现m次

(xy){n}xy #关键字出现n次

.* #关键字之间匹配任意字符

要加-E,例如:

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

grep正则 匹配次数_linux_02


grep正则 匹配次数_linux_03


例子:

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

[root@localhost mnt]# 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@localhost mnt]# sh user_check.sh

grep正则 匹配次数_grep正则 匹配次数_04


脚本内容

grep正则 匹配次数_grep正则 匹配次数_05

2.sed 行编辑器

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

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

grep正则 匹配次数_sed_06


grep正则 匹配次数_grep正则 匹配次数_07


grep正则 匹配次数_grep正则 匹配次数_08


例子:

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

[root@localhost mnt]# 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@localhost mnt]# sh user_create.sh userfile passwordfile

grep正则 匹配次数_sed_09


脚本内容

grep正则 匹配次数_linux_10


2)a 添加(默认添加到关键字下面一行)

[root@localhost mnt]# sed -e '/hello/aworld' file

3)i 插入(默认插入到关键字上面一行)

[root@localhost mnt]# sed -e '/hello/iworld' file

4)c 替换

[root@localhost mnt]# sed -e '/hello/cworld' file

5)d 不显示

[root@localhost mnt]# sed -e '/^#/d;/^$/d' fstab

grep正则 匹配次数_linux_11


6)w 写入

[root@localhost mnt]# sed '/^UUID/w file' fstab                #保存到文件并显示
[root@localhost mnt]# sed -n '/^UUID/w file' fstab             #保存到文件不显示

grep正则 匹配次数_正则表达式_12


7)sed其他用法

[root@localhost mnt]# sed '/^UUID/= ' fstab                    #显示UUID的行数,结果和内容
[root@localhost mnt]# sed -n -e '/^UUIP/p' -e '/^UUID/= ' fstab#显示UUID的行数,只显示结果
[root@localhost mnt]# sed 'G' fstab                            #每行后加空行
[root@localhost mnt]# sed '$!G' fstab                          #每行后加空行,不包括最后一行
[root@localhost mnt]# sed 's/nologin/linux/g' passwd           #替换,g表示全文替换
[root@localhost mnt]# sed -e '/adm/,/sync/s/nologin/linux/g' passwd 
[root@localhost mnt]# sed 's/nologin/linux/g' -i  passwd       #替换并保存

grep正则 匹配次数_正则表达式_13


grep正则 匹配次数_正则表达式_14


grep正则 匹配次数_sed_15


grep正则 匹配次数_sed_16


grep正则 匹配次数_正则表达式_17


grep正则 匹配次数_正则表达式_18

3.awk 报告生成器

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

-F #指定分隔符
[root@localhost mnt]# awk -F : '/^{a-d}/{print $1,$6}' passwd
[root@localhost mnt]# awk -F : '/^a|nologin$/{print $1 ,$7}' passwd
[root@localhost mnt]# awk -F : '$6~/bin$/{print $1,$6}' passwd
[root@localhost mnt]# awk -F : '$7!~/nologin${print $1 ,$7}' passwd

grep正则 匹配次数_grep正则 匹配次数_19


grep正则 匹配次数_grep正则 匹配次数_20


grep正则 匹配次数_grep正则 匹配次数_21


awk测试:

抓取eth0网卡的ip
[root@localhost mnt]# ifconfig eth0 | awk -F " " '/inet\>/{print $2}'
统计passwd文件有多少行
[root@localhost mnt]# awk -F : 'BEGIN{N=0}{N++}END{print N}' passwd

grep正则 匹配次数_正则表达式_22