三剑客
01.grep
作用:模糊过滤文件内容
语法结构:
grep '文件内容' file
其他命令输出结果|grep '字符串'
| 参数 | 作用 |
|---|---|
-v |
取反 |
-r |
递归过滤 |
-w |
精确匹配 |
-E |
支持扩展正则egrep |
-n |
显示过滤内容的行号 |
-o |
匹配过滤 |
-c |
统计单词个数 |
-i |
忽略大小写 |
-A |
过滤到行的向下n行 |
-B |
过滤到行的向上n行 |
-C |
过滤到行的上下各n行 |
02.sed
作用:
- 模糊过滤
- 删除行
- 替换
- 反向引用
1.sed找行
`语法结构`
sed -n 'np' file
`示例`
sed -n '3,5p' file
sed -n '3,$p' file
2.模糊过滤
`语法结构`
sed -n '/字符串/p' file
`示例`
sed -n '/^a/p' file
sed -n '/xx$/p' file
sed -n '/xx/,/xx/p' file
3.删除
`语法结构`
sed '3d' file
sed '/字符串/d' file
4.增加保存
`语法结构`
sed '3c aaa' file
sed '3i aaa' file
sed '3a bbbb' file
sed '3w newfile' file
5.替换
`语法结构`
sed 's#a#b#g' file # 替换文件中所有的a为b
sed '3s#a#b#g' file # 第3行的a换成b
sed '/root/s#oldboy#root#g' # 替换包含root行的oldboy为root
vim替换
:%s///g
6.反向引用
`语法结构`
sed -r 's#()#1#g'
`示例`
echo "mysql admin" | sed -r 's#([^ ]+) ([^ ]+)#2 1#g'
03.awk
`内置变量`
NR: 行号
NF: 最后一列列号
$0: 每行
, : 空格
FS: 指定默认分隔符
, : 空格
OFS: 映射是逗号
1.awk取行
awk 'NR==5' file # 取出第5行
awk 'NR>5' file # 大于5的行
awk 'NR>5&&NR<8' file # 大于5并且小于8的行
awk 'NR<5||NR>8' file # 小于5或者大于8的行
2.awk去列 默认按空格或者tab键为分隔符
awk '{print $1}' file # 输出第一列
awk '{print $NF}' file # 输出最后一列
awk '{print NF}' file # 输出最后一列列号
awk '{print $(NF-1)}' file # 输出倒数第2列
#指定分隔符
awk -F "分隔符" file
awk -F "[]+" file
3.模糊过滤
grep 'root' file
sed -n '/root/p' file
awk '/root/' file # awk '/root/{print}' file
awk '/^r/' file
awk '/a/,/b/' file
4.按列进行匹配
awk '$2 ~ /^r/' file
`示例`
#匹配最后一列以f开头的行
root@isre:~/test# awk -F/ '$NF ~ /^f/' 1.txt
mysql:x:108:115:MySQL Server,,,:/nonexistent:/bin/false
#第三列匹配3的行
root@isre:~/test# awk -F: '$3 ~ /^3$/' 1.txt
sys:x:3:3:sys:/dev:/usr/sbin/nologin
5.以列进行数值比对
awk '$3==0' 1.txt
awk -F: '$3>5' 1.txt
`示例`
统计成绩
姓名 成绩
awk '$2<=60' file|wc -l # 统计小于等于60分的同学的个数
awk '$2>60&&$2<90' file|wc -l
6.模式+动作
awk 'NR==1{print $1}' file # 输出第一行的第一列
awk '/字符串/{print $NF}' file # 输出包含字符串的行的最后一列
7.awk运算
awk 'BEGIN{print 10+10}'
awk 'BEGIN{print 10-10}'
字符串比对
awk -F: '$1=="root"' file
8.使用printf格式化输出
/test# awk -F: '{printf "%-15s %-15sn",$1,$NF}' 1.txt
root /bin/bash
daemon /usr/sbin/nologin
bin /usr/sbin/nologin
sys /usr/sbin/nologin
sync /bin/sync
games /usr/sbin/nologin
man /usr/sbin/nologin
lp /usr/sbin/nologin
9.awk-if判断
`单条件`
root@isre:~/test# awk -F: '{if($1=="root"){print}}' 1.txt
root:x:0:0:root:/root:/bin/bash
`多条件`
root@isre:~/test# awk -F: '{if($1=="root"){print}else if(0){}else if(0){}else{}}' 1.txt
root@isre:~/test# awk -F: '{if($3==0){print $1} else if ($3==3){print $NF} else {print $0}}' 1.txt
root
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
#判断第三列等于0的行
root@isre:~/test# awk -F: '{if($3==0){a++}}' /etc/passwd
root@isre:~/test#
#读取文件结束后,输出a的值
root@isre:~/test# awk -F: '{if($3==0){a++}}END{print a}' /etc/passwd
1
root@isre:~/test# cat a.txt
m
f
m
m
f
x
root@isre:~/test# awk '{if($1=="m"){a++}}END{print a}' a.txt
3
root@isre:~/test# awk '{if($1=="m"){a++} else if ($1=="f"){b++}}' a.txt
root@isre:~/test# awk '{if($1=="m"){a++} else if ($1=="f"){b++}}END{print a,b}' a.txt
3 2
root@isre:~/test# awk '{if($1=="m"){a++} else if ($1=="f"){b++}}END{print "m出现了"a "nf出现了"b}' a.txt
m出现了3
f出现了2
root@isre:~/test# awk -F: '{if($3==0){a++}else if($3>0&&$3<1000){b++}else{c++}}END{print "管理员的个数: "a "n虚拟用户的个数: "b"n普通用户的个数: "c}' /etc/passwd
管理员的个数: 1
虚拟用户的个数: 29
普通用户的个数: 1
10.awk数组案例
root@isre:~/test# awk -F: '{shell[$NF]++}' /etc/passwd
root@isre:~/test# #a++ a=a+1 a=1
root@isre:~/test# #a++ a=1+1 a=2
root@isre:~/test# #/bin/bash++ /bin/bash=bin/bash+1 /bin/bash=1
root@isre:~/test# #/bin/bash++ /bin/bash=2
root@isre:~/test# awk -F: '{shell[$NF]++}END{for(i in shell)print i,shell[i]}' /etc/passwd
/bin/sync 1
/bin/bash 1
/bin/false 1
/usr/sbin/nologin 28
root@isre:~/test# cat 3.txt
10.0.0.1 GET 200
10.0.0.1 GET 200
10.0.0.1 GET 200
10.0.0.1 GET 301
10.0.0.1 GET 304
10.0.0.1 GET 304
192.168.1.100 POST 304
192.168.1.100 POST 304
192.168.1.100 POST 304
192.168.1.100 POST 304
192.168.1.100 POST 304
192.168.1.100 POST 304
192.168.1.100 POST 500
192.168.1.100 POST 500
192.168.1.100 POST 404
root@isre:~/test# awk '{st[$1" "$3]++}' 3.txt
root@isre:~/test# awk '{st[$1" "$3]++}END{for(i in st)print i}' 3.txt
192.168.1.100 404
192.168.1.100 500
10.0.0.1 200
192.168.1.100 304
10.0.0.1 301
10.0.0.1 304
root@isre:~/test# awk '{st[$1" "$3]++}END{for(i in st)print st[i],i}' 3.txt
1 192.168.1.100 404
2 192.168.1.100 500
3 10.0.0.1 200
6 192.168.1.100 304
1 10.0.0.1 301
2 10.0.0.1 304
#同时将多列的值所做索引自增
root@isre:~/test# cp /var/log/nginx/access.log .
root@isre:~/test# awk '{st[$1" "$9]++}END{for(i in st)print st[i],i}' access.log
1 66.132.195.37 301
1 45.56.79.53 200
1 43.165.186.188 200
1 45.198.224.7 301
1 3.224.215.150 200
6 113.215.189.206 200
正文完