shell中的常用命令(grep,sed,awk)

一.grep(过滤)

1.grep

grep -E=egrep

2.grep格式

grep  匹配条件   处理文件

grep   root   passwd            #过滤root关键字

grep  ^root   passwd            #以root开头

grep  root$   passwd            #以root结尾

grep  -i root passwd            #忽略大小写

grep -E "\<root" passwd         #root字符之前不能有字符

grep -E "root\>" passwd         #root字符之后不能有字符

grep -数字                      #显示过滤行以及上面几行和下面几行

grep -n                         #显示匹配的行所在行号

grep -A                         #显示过滤行以及下面几行

grep -B                         #显示过滤行以及上面几行

grep -v                         #反向过滤

shell中的常用命令(grep,sed,awk)

3.grep字符数量匹配规则

^westos                #以westos开头

westos$                #以westos结尾

w....s                 #w开头s结尾中间4个任意字符

.....s                 #s结尾前面5个任意字符

*                      #字符出现任意次

?                     #0到1次

+                      #1次到任意次

{n}                    #字符出现n次

{m, n}                 #m到n次

{0, n}                 #0-n次

{,n}                   #0-n次

{m,}                   #最少m次

(tod){2}               #lee字符串出现2次

shell中的常用命令(grep,sed,awk)

shell中的常用命令(grep,sed,awk)

练习:统计在系统中能su-切换的用户,显示名称

shell中的常用命令(grep,sed,awk)

二.sed

1.命令格式:

sed 参数 命令 处理对象

sed 参数 处理对象 -f 处理规则文件

2.对字符的处理

(1)p

sed -n   3p     mima    #显示第3行

sed -n   3,5p   mima    #显示3-5行

sed -ne '3p;5p' mima    #显示3和5行    

sed -ne  '5,$p' mima    #显示5到最后行

sed -n  '/^root/p' mima   #显示以#开头的行

shell中的常用命令(grep,sed,awk)

(2)d

sed   5d      mima    #删除第五行

sed '/^#/d'   mima   #把#开头的行删除

sed '/^w/!d'  mima    #除了w开头的行,其余行都删除

sed -e '5,$d' mima    #删除5到最后行

shell中的常用命令(grep,sed,awk)

(3)a

sed -e '$a hello world'    tests

sed -e '$a hello\nworld'   tests

sed -e '/^#/a hello world' tests

shell中的常用命令(grep,sed,awk)

(4)c

sed -e '/^h/c hello world' tests

sed '5c hello world' tests

(5)w

sed '/^h/w testnew' tests #把tests中h开头的行写入testnew中

shell中的常用命令(grep,sed,awk)

(6)r

sed '1r new' tests  #把new里内容整合到tests第一行后

sed '2r new' tests  #把new里内容整合到testse第二行后

shell中的常用命令(grep,sed,awk)

(7)sed字符替换

sed 's/:/##/g'  mima

sed 's/:/##/'   mima

sed 's/:/##/g'   mima

sed '1,5s/:/##/g'   mima

sed '1s/:/##/g'   mima

sed '1s/:/##/g;5s/:/##/g'   mima

sed '/lp/,/shutdown/s/:/##/g'   mima

sed 's/\//##/g'   mima

sed '[email protected]/@##@g'   mima   

shell中的常用命令(grep,sed,awk)

shell中的常用命令(grep,sed,awk)

练习:Apache.sh 此脚本后接入数字,http的端口就改为此数字,

建设selinux为关闭状态

shell中的常用命令(grep,sed,awk)

shell中的常用命令(grep,sed,awk)

三.awk

awk -F : 分隔符 BEGIN{}{}END{} FILENAME

NR                        ##行数

NF                        ##列数

FILENAME                  ##文件名称本身

westos                    ##westos变量值

"westos"                  ##westos字符串

/bash$/                    #条件

/条件1|条件2/              ##条件1或者条件2

/条件1/||/条件2/           ##条件1或者条件2

/条件1/&&/条件2/           ##条件1并且条件2

$0                        #所有的列

$1                        #第一列

$2                        #第二列

shell中的常用命令(grep,sed,awk)

shell中的常用命令(grep,sed,awk)

shell中的常用命令(grep,sed,awk)


 练习:统计在系统中能su切换的且用户家目录不在/home下的用户数量

awk -F : "BEGIN{N=0}$6!/^\/HOME/&&/bash$|sh$tcsh$/{N++}END{print N}' /etc/passwd