正则表达式
基本正则
扩展正则
per兼容的正则
实例1
[root@root shell]# grep -P "\bbin\b" /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
grep 语法格式
grep [选项] 匹配模式[文件]
常用选项
-i 忽略大小写
-v 只打印没有匹配的,而匹配的反而不打印。
-w 匹配单词
-q 静默匹配,不将结果显示在屏幕上
sed语法
什么是sed?
流式编辑器(一次处理一行),具有非交互式,逐行处理,可以对文本进行增、删、改、查等操作。
语法格式
sed [选项] ‘[定位符]指令’ 文件名
命令 | sed [选项] ‘[定位符]指令’
常用命令选项
-n :屏蔽默认输出
-i : 直接修改源文件
-r :支持扩展正则
实例1:数据定位
[root@root shell]# sed -n '3p' /etc/passwd # 打印第3行
[root@root shell]# sed -n '1,3p' /etc/passwd #打印1到3行
[root@root shell]# sed -n "1~2p" /etc/passwd #第1行开始,步长为2行
[root@root shell]# sed -n "2,+3p" /etc/passwd #第2行以及后面的3行
实例2:正则定位
[root@root shell]# grep ^root /etc/passwd #过滤root开头的行
[root@root shell]# sed -n '/^root/p' /etc/passwd
[root@root shell]# egrep "[0-9]{3}" /etc/passwd #过滤包含3个数字的行
[root@root shell]# sed -rn "/[0-9]{3}/p" /etc/passwd
常用sed指令1
p(print):打印行
d(delete): 删除行
c(replace):替换行
s(substitution):替换关键词
= :打印行号
实例1:打印数据(Print指令)
[root@root shell]# sed -n "/DEVICE/p" /etc/sysconfig/network-scripts/ifcfg-ens32 #过滤网卡ip地址
[root@root shell]# free | sed -n "/Mem/p" #查看内存信息
[root@root shell]# df -h | sed -n "/\/$/p" #过滤磁盘根分区信息
[root@root shell]# sed -n "1p;3p;6p" /etc/passwd #现实第1,3,6行内容
[root@root shell]# sed -n "2!p" /etc/hosts #打印第2行以外其他行
实例2:删除数据(delete 指令,不使用-i 选项,源文件不会被修改)
[root@root shell]# sed "d" /etc/hosts #删除/etc/hosts全文,没有定位条件等于匹配所有行
[root@root shell]# sed "1,3d" /tmp/fstab #删除1-3行
[root@root shell]# sed '/dev/!d' /tmp/fstab #不包含dev行都删除
[root@root shell]# sed '/^#/d' /tmp/fstab #删除所有以#符号开头的行
[root@root shell]# sed '/^$/d' /tmp/fstab #删除空白行
实例3:替换行(Replace指令,不使用-i ,源文件不会被修改)
[root@root shell]# sed 'c 123456' /tmp/fstab #所有的行为123456
[root@root shell]# sed 'DEVICE/c EDVICE= ens33' $file #替换设备名称
[root@root shell]# sed '/127/c 127.0.0.1 localhost' /etc/hosts
实例4:替换关键词(Substitution,不使用-i选项,源文件不会被修改)
c以行为单位,替换 s以关键词为单位替换
文本day3
[root@root shell]# cat day3
2046 2048 2046 2046
1001 2046 2999 1889
2046 2046 2046 2046
[root@root shell]# sed 's/2046/XXXX/' day3 #替换首行的2046
[root@root shell]# sed 's/2046/XXXX/g' day3 #替换全部的2046
[root@root shell]# sed 's/2046/XXXX/3' day3
[root@root shell]# sed '3s/2046/(&)/g' day3 #(&)中的&代表前面的2046
[root@root shell]# sed '2s/2046/(&)/g' day3 #只是替换第2行的2046
[root@root shell]# sed '3s/2046//g' day3 #将第三行2046替换成空格
[root@root shell]# sed -n '3s/2046/(&)/p' day3 #替换第三行并打印
其中Substitution指令(替换符/可以用其他的字符)
[root@root shell]# sed 's#2046#XXXX#g' day3
实例5:显示行号(=)
[root@root shell]# sed -n '/root/=' /etc/passwd #显示含有root的行号
[root@root shell]# sed -n '$=' /etc/passwd #统计行号
[root@root shell]# wc -l /etc/passwd
[root@root shell]# echo "hello the wold" | sed -r 's/^(.)(.*)(.)$/\3\2\1/'
dello the wolh
()具有赋值保留的作用,()和后面的数字有关,第一个()代表1
常用sed指令2
i(insert):插入
a(apend):追加
r(read):读取文件导入文件内容
w(write):文件另存为|导出文件内容
实例6:插入指令(insert,插入,行前写入)
[root@root shell]# sed '2i ABC_XYZ' day3 #在第2行前面插入ABC_XYZ
[root@root shell]# sed '/2046/i ABC\nXYZ' day3 #在2046前面插入ABC \n(换行)XYZ
实例7:追加(Append,追加,行后写入)
[root@root shell]# sed '2a ABC_XYZ' day3 #在第2行后面添加ABC_XYZ
[root@root shell]# sed '/2046/a ABC\nXYZ' day3 #在2046后面添加ABC和XYZ
实例8、Read(将其他文件的内容导入)
[root@root shell]# sed '2r /etc/hosts' day3 #将/etc/hosts 文件的前两行读入到day3中首行
[root@root shell]# sed 'r /etc/hosts' day3 #将/etc/hosts 文件的每一行都读取到day3中
实例9、将文件内容导出另存到其他文件
[root@root shell]# sed 'w copy_day3' day3 #将day3文件的所有内容另存为一个新文件copy_day3
[root@root shell]# sed '/1889/w 123_day3' day3 #将day3文件中所有包含1889的行另存为一个新文件123_day3
[root@root shell]# sed '2,3w 1_day3' day3 #将day3文件中的2到3行另存为新文件1_day3
实例10、随机点名脚本
姓名准备
[root@root shell]# ls
day day2 day3 day4 name
[root@root shell]# cat name
libai
dufu
baijuyi
menhaoran
shushi
#! /bin/bash
name_file="name"
line_file=$(sed -n '$=' $name_file)
while :
do
clear
tmp=$(sed -n "$[RANDOM%line_file+1]p" $name_file)
# tmp=$(sed -n "$[RANDOM % $line_file+1]p" $name_file)
echo -e "\033[32m ctrl+c stop: \033[0m"
echo -e "\033[32m ################## \033[0m"
echo -e "\033[32m # #\033[0m"
echo -e "\033[37m $tmp \033[0m"
echo -e "\033[32m # #\033[0m"
echo -e "\033[32m ################### \033[0m"
sleep 0.5
done
实例11、抓取网页图纸
#! /bin/bash
#功能描述,抓取单个网页中的图片数据
URL="/root/shell/img"
#将网页源码保存到文件当中
curl -s http://www.tmooc.cn/ > $URL
#对文件进行过滤和清洗,获取需要的种子URL链接
echo -e "\033[32m URL is downling \033[0m"
sed -i '/<img/!d' $URL #删除不包含<img的行
sed -i 's/.* src="//' $URL #删除sr=“以及前面的所有内容
sed -i 's/".*//' $URL #删除双引号以及后面的所有内容
echo
if ! rpm -q wget &> /dev/null;
then
yum -y install wget
if
#wget 为下载工具,-P 指定将数据下载到特定目录,-q不显示下载过程
echo -e "\033[32m please waitting \033[0m"
for i in $(cat $URL)
do
wget -P /tmp/ -q $i
done
rm -rf $URL
awk 编程语言
基本语法格式
1、前置命令 | awk [选项] '[条件]{指令}' #指令中多条语句使用;
2、awk [选项] '[条件] {指令}' 文件
3、awk 'BEGIN{} [条件]{} END{}' 文件
实例1
[root@root shell]# cat day5
hello the world
welcome to Beijing
[root@root shell]# awk '{print $1,$3}' day5
hello world
welcome Beijing
实例2:-F可以指定分割符,默认分隔符为tab或者空格
[root@root shell]# awk -F: '{print $1,$3}' /etc/passwd #指定分隔符为: 打印/etc/passwd中的第1行和第3行
root 0
bin 1
实例3:内置变量都有特殊含义,可直接使用
$n:指定分隔的第n个字段,如$1、$3分别表示第1、第3列
$0:当前读入的整行文本内容
NF:列数
NR:行数
[root@root shell]# awk -F: '{print NR,NF}' /etc/passwd
1 7
2 7
[root@root shell]# awk '{print $NF}' day5
world
Beijing
[root@root shell]# awk '{print $(NF-1)}' /etc/passwd
[root@root shell]# awk -F: '{print "name:",$1, "bash:",$7}' /etc/passwd
实例4:awk过滤的时机
在所有行前处理,BEGIN{}
读入第一行文本之前执行
一般用来初始化操作
逐行处理{}
逐行读入文本执行相应的处理
在所有行后处理,END{}
处理完最后一行文本之后执行
一般用来输出处理结果
以上处理方式可以单独使用,也可以一同使用
实例5
[root@root shell]# awk 'BEGIN {a=34;print a+12}'
46
[root@root shell]# awk 'BEGIN {x=0} /bash$/{x++} END{print x}' /etc/passwd
5
[root@root shell]# awk -F: 'BEGN{print NR} END{print NR}' /etc/passwd
51
实例6、查看内存
[root@root shell]# free | awk '{print $NF}'
available
112408
1142524
[root@root shell]#
[root@root shell]# free | grep Mem | awk '{print $NF}'
112040
[root@root shell]# free | awk '/Mem/{print $NF}'
112192
实例7:查看磁盘信息
[root@root shell]# df | grep "\/$" | awk '{print $4}'
[root@root shell]# df | awk '/\/$/{print $4}'
实例8:过滤CPU信息
[root@root shell]# lscpu | grep "Model name" | awk -F: '{print $2}'
[root@root shell]# lscpu | grep "^CPU(s)" | awk -F: '{print $2}'
[root@root shell]# uptime | awk '{print $NF}'
实例9:过滤网卡流量
[root@root shell]# ifconfig ens32 | awk '/RX p/{print "ru zhan: " $5 "byte"}'
[root@root shell]# ifconfig ens32 | awk '/TX p/{print "chu zhan: " $5 "byte"}'
实例10:监控暴力破解的IP地址
[root@root shell]# grep "Failed" /var/log/secure | awk '{print $11}'
[root@root shell]# awk '/Failed/{print $11}' /var/log/secure
awk条件判断
基本语法
awk [选项] ‘条件{指令}’ 文件
条件 表达式
正则表达式、数值/字符比较、逻辑比较
实例1:条件设置之正则
正则:/正则表达式/ ~匹配,!~不匹配
[root@root shell]# awk -F: '/^ro/{print}' /etc/passwd
[root@root shell]# awk -F: '$7!~/bash$/{print $1,$7}' /etc/passwd
实例2:数值、字串比较
== 等于 !=不等于
> 大于 >= 大于或等于
< 小于 <=小于或等于
[root@root shell]# awk 'NR==2{pirnt}' /etc/passwd
[root@root shell]# awk -F: '$7!="/bin/bash"' /etc/passwd
[root@root shell]# awk -F: '$3>=1000{print $1}' /etc/passwd
实例3:逻辑比较测试
&& 逻辑与:期望多个条件都成立
|| 逻辑或: 只要一个条件成立即可满足要求
[root@root shell]# awk -F: '$3>=0 && $3<2{print $1,$3}' /etc/passwd
[root@root shell]# awk -F: '$3>=1 || $3<7 {print $1,$3}' /etc/passwd
实例4:运算符
+ - * / % ++ -- += -= *= /=
[root@root shell]# awk 'NR%2==1{print}' /etc/passwd
[root@root shell]# seq 200 | awk 'BEGIN{i=0} ($0%3==0)&&($0%13==0){i++} END{print i}'
5
实例5:单分支if判断
awk '{指令}' 文件
if(){指令}
[root@root shell]# awk -F: '{if($3>=100){i++}} END{print i}' /etc/passwd
[root@root shell]# awk -F: '{if($1=="root") {print $1,$3}}' /etc/passwd
[root@root shell]# awk -F: '{if($NF>0.01) {print "CPUload: " $NF}}' /etc/passwd
实例6:双分支if判断
awk '{指令}' 文件
if(){指令}else{}
[root@root shell]# awk -F: '{if($3>=1000){i++}else{j++}} END{print "puUser:"i,"systemUser:"j}' /etc/passwd
[root@root shell]ls -l /etc/ | awk '{
if($1~/^-/){x++} else{y++}
}
END {print "普通文件个数:"x,"目录个数:"y}'
实例7:多分支if判断
语法格式
awk '{指令}' 文件
if(){指令}else if(){}...else{}
[root@root shell]ls -l /etc/ | awk '{
if($1~/^-/){x++} else if ($1~/^d/){y++} else{z++}
}
END {print "普通文件个数:"x,"目录个数:"y,"其他个数:"z}'
实例8:awk的for循环
语法格式
for(表达式1;表达式2;表达式3){指令}
[root@root shell]# awk 'BEGIN{for(i=1;i<=5;i++){print i}} '
[root@root shell]# awk 'BEGIN{for(i=5;i>=1;i--){print i}}'
awk数组定义以及使用
定义数组
格式:数组名[下标]=元素值
调用数组
格式:数组名[下标]
遍历数组
格式:for (变量名 in 数组名){ print 数组名[变量])
实例1:
数组赋值
[root@root shell]# awk 'BEGIN { name[1]="jimi"; name[2]="xiaohong"; print name[1], name[2]}'
jimi xiaohong
数组遍历
[root@root shell]# awk 'BEGIN {x[0]=0; x[1]=1;x[2]=2;x[3]=3;for(i in x){print x[i]}}'
实例2:统计ip访问次数
[root@root shell]# awk '{IP[$1]++} END{for (i in IP){print i,IP[i]} }' day5
Ip[$1]=0
Ip[$1]=ip[$1]+1
Ip[18]=0
Ip[18]=1
Ip[17]=0
Ip[17]=1
Ip[18]=ip[18]+1=2
实例3:ss命令
ss命令可以用来获取socket统计信息
参考文章
TCP_Total=$(ss -s | awk '$1=="TCP"{print $2}')
UDP_Total=$(ss -s | awk '$1=="UDP"{print $2}')
TCP_Listen_Total=$(ss -antlpH | awk 'BEGIN{count==0} {count++} END{print count}')
TCP_Estab_Total=$(ss -antlpH | awk 'BEGIN{count==0} /^ESTAB/{count++} END{print count}')
TCP_TIME_WAIT_Total=$(ss -antpH | awk 'BEGIN{count==0} /^TIME-WAIT/{count++} END{print count}')
echo "UDP:" $UDP_Total
echo "TCP:" $TCP_Total
echo "TCP_Listen_Total:"$TCP_Listen_Total
echo "TCP_Estab_Total:" $TCP_Estab_Total
echo "TCP_TIME_WAIT_Total:" $TCP_TIME_WAIT_Total