grep 命令
grep ^vivek /etc/passwd
grep -E ‘word1|word2’ 文件名
ping -qc1 google.com 2>&1 | awk -F’/’ ‘END{ print (/^rtt/? “OK “$5” ms”:”FAIL”) }’
ping -qc1 : 不输出ping 的过程; 2>&1 : 将标准警告(2)重定向到标准输出(1); awk -F’/’ 定位最后一行(END)如果是以rtt开头 输出OK 并打印$5,如果是其他的信息,输出FAIL。
ping -qc4 47.240.25.22 2>&1 |grep rtt |awk -F ‘[/= ]’ ‘{print “ avg:” $9,”\n”,”mdev:” $11}’
if [[ `ping 47.240.25.22 -c 4 |grep rtt |awk -F ‘[/= ]’ ‘{print $11}’` -lt 1 ]];then echo “ok”;else echo “ERROR”;fi
N=1; ping google.com -n 10 | grep Minimum | sed -r ‘s/(Average = [[:digit:]]+ms)/\1\n/g’ | sed -r s’/[[:space:]]+(Minimum)/\1/g’ | while read file; do echo Server “$N”: “$file”; N=$((N+1)); done » output.txt
-
The steps:
-
The first sed fixes the newline issue: Match the final part of the string after which you want a new line, in this case Average = [[:digit:]]+ms and put it into a group using the parenthesis Then replace it with the same group (\1) and insert a newline character (\n) after it
- 1.1 第一个sed修复换行问题:匹配字符串的最后一部分,在此之后你需要一个新行,在这种情况下,Average = [[:digit:]] + ms并使用括号将其放入一个组然后将其替换为 同一个组(\ 1)并在它之后插入换行符(\ n)
-
The second sed removes the whitespaces, by matching the word Minimum and all whitespaces in front of it after which it only returns the word Minimum The final while statement loops over each line and adds Server “$N”: in front of the ping results. The $N was initialized to 1 at the start, and is increased with 1 after each read line
- 2-1 第二个sed删除空格,通过匹配单词Minimum和它前面的所有空格,之后它只返回单词Minimum the final while语句循环 在每一行上添加服务器“$ N”:在ping结果前面。 $ N在开始时初始化为1,并且在每条读取线后增加1
-
awk 命令
substr 截取字符串
- substr(s,p) 返回字符串s中从p开始的后缀部分
-
substr(s,p,n) 返回字符串s中从p开始长度为n的后缀部分
- awk -F ‘,’ ‘{print substr($3,6)}’
表示以”,”分割 从第3个字段里的第6个字符开始,一直到设定的分隔符”,”结束.
- substr($3,10,8)
表示是从第3个字段里的第10个字符开始,截取8个字符结束.
- substr($3,6)
表示是从第3个字段里的第6个字符开始,一直到结尾
- eg:
获取eth0 IP 地址
ifconfig eth0 | sed -n “2p” | awk ‘{print substr($2,1)}’
- eg:
echo “123” | awk ‘{print substr($0,1,1)}’ output: 1
#!/bin/bash
time1=`date "+%Y-%m-%d %H:%M:%S"`
#time1=`date -d"$A" +%s`
echo " ___________________________________________" >> cpus.txt
echo "| |" >> cpus.txt
cpu_info=`cat /proc/cpuinfo | grep "model name" | uniq | awk -F ":" '{print $2}' |sed 's/[ ][ ]*/ /g'`
echo "|$cpu_info |">> cpus.txt
echo "| `free -h | grep "Mem" | awk '{print "Mem_totle: "$2}'`" >> cpus.txt
echo "|********CPU_Idle(%) and Mem_free(%)********|" >> cpus.txt
echo "|******Start Time:$time1*******| " >> cpus.txt
totle=`free -m | grep -i 'Mem'| awk -F ' ' '{print $2}'`
sync; echo 1 > /proc/sys/vm/drop_caches # 清空缓存
if [ ! -d "tmp" ]; then
mkdir "tmp"
fi
#cat /dev/null > cpus.txt
for i in {1..10}
do
if [ $i -lt 10 ];then
# A=`top -b -n 1 | grep %Cpu | awk -F ',' '{print $4}' | awk '{print $1}'`
A=`top -b -n 2 | grep "%Cpu" | awk 'NR==2' | awk -F ' ' '{print $8}'`
# B=`top -b -n 1 | grep -i 'KiB Mem' | awk -F ',' '{print $1}' | awk '{print $4}'`
C=`free -m | grep -i 'Mem'| awk -F ' ' '{print $4}'`
# D=`expr $C / $B`
echo $C >> tmp/tmp_Mem.txt
echo $A >> tmp/tmp_Idle.txt
i=`expr $i + 1`
# sleep $1
else
# B=`awk -F' ' '{sum+=$1;count+=1} END{print "SUM:"sum"\nAVG:"sum/count}' tmp.txt`
# B=`awk -F' ' '{sum+=$1;count+=1} END{print "AVG:"sum/count}' tmp.txt`
E=`awk '{sum+=$1} END {print "CPU_idle_Avg: ", sum/NR}' tmp/tmp_Idle.txt`
F=`awk 'BEGIN {max = 0} {if ($1+0 > max+0) max=$1} END {print "CPU_idle_Max: ", max}' tmp/tmp_Idle.txt`
G=`awk 'BEGIN {min = 65536} {if ($1+0 < min+0) min=$1} END {print "CPU_idle_Min: ", min}' tmp/tmp_Idle.txt`
# H=`awk '{sum+=$1} END {print "CPU_Mem_Avg: ", sum/NR/7810}' tmp_Mem.txt`
H=`awk -v totle=$totle '{sum+=$1} END {print "Mem_Avg: ", (sum/NR/totle)*100}' tmp/tmp_Mem.txt`
I=`awk -v totle=$totle 'BEGIN {max = 0} {if ($1+0 > max+0) max=$1} END {print "Mem_Max: ", (max/totle)*100}' tmp/tmp_Mem.txt`
J=`awk -v totle=$totle 'BEGIN {min = 65536} {if ($1+0 < min+0) min=$1} END {print "Mem_Min: ", (min/totle)*100}' tmp/tmp_Mem.txt`
echo " $E " >> cpus.txt
echo " $F " >> cpus.txt
echo " $G " >> cpus.txt
echo " ----------" >> cpus.txt
# echo " `free -m | grep "Mem" | awk '{print "Mem_totle: "$2}'`" >> cpus.txt
echo " $H " >> cpus.txt
echo " $I " >> cpus.txt
echo " $J " >> cpus.txt
fi
done
cat /dev/null > tmp/tmp_Idle.txt
cat /dev/null > tmp/tmp_Mem.txt
time2=`date "+%Y-%m-%d %H:%M:%S"`
Times1=`date -d"$time1" +%s`
Times2=`date -d"$time2" +%s`
time3=`expr $Times2 - $Times1`
time4=`date -d @$time3 +%M:%S`
echo "|*******End Time:$time2********|" >> cpus.txt
echo "|*************Duration($time4)***************|" >> cpus.txt
echo "|___________________________________________|" >> cpus.txt
echo -e " \n " >> cpus.txt
sleep 2
cat cpus.txt