shell脚本中打印所有的匹配某个字符串的行或前后各N行

在日常运维中,经常需要监控某个进程,并打印某个进程的监控结果,通过需要打印匹配某个结果的行以及其前后各N行。

注意:echo使用-e参数,对打印的结果中进行换行

[root@mq-master02 ~]# echo "abcd"
abcd
[root@mq-master02 ~]# echo "ab\ncd"
ab\ncd
[root@mq-master02 ~]# echo "ab \n cd"
ab \n cd
[root@mq-master02 ~]# echo -e "ab\ncd"
ab
cd
[root@mq-master02 ~]# echo -e "ab \n cd"
ab 
 cd
[root@mq-master02 ~]# echo -e "ab \n cd \n \n df"
ab 
 cd 
 
 df
 [root@mq-master02 ~]# echo -e "ab\ncd \n \ndf"   
ab
cd 
 
df

1)案例一

[root@mq-master02 ~]# cat /opt/test 
192.168.10.11
Don't worry! main is running!
192.168.10.12
Don't worry! main is running!
192.168.10.13
It's so bad! main is failed!
192.168.10.14
Don't worry! main is running!
192.168.10.15
Don't worry! main is running!
192.168.10.16
It's so bad! main is failed!
192.168.10.17
Don't worry! main is running!
192.168.10.18
Don't worry! main is running!
192.168.10.19
Don't worry! main is running!
192.168.10.20
Don't worry! main is running!
192.168.10.21
Don't worry! main is running!
192.168.10.12
Don't worry! main is running!

1)打印/opt/test中所有匹配"main is failed"的行
[root@mq-master02 ~]# cat /opt/test |grep "main is failed"
It's so bad! main is failed!
It's so bad! main is failed!
[root@mq-master02 ~]# sed -n '/main is failed/p' /opt/test
It's so bad! main is failed!
It's so bad! main is failed!

2)打印/opt/test中所有匹配"main is failed"的行及其前1行
[root@mq-master02 ~]# cat /opt/test |grep "main is failed" -B1   
192.168.10.13
It's so bad! main is failed!
--
192.168.10.16
It's so bad! main is failed!

3)打印/opt/test中所有匹配"main is failed"的行及其后1行
[root@mq-master02 ~]# cat /opt/test |grep "main is failed" -A1
It's so bad! main is failed!
192.168.10.14
--
It's so bad! main is failed!
192.168.10.17

4)打印/opt/test中所有匹配"main is failed"的行及其前后各1行
[root@mq-master02 ~]# cat /opt/test |grep "main is failed" -C1
192.168.10.13
It's so bad! main is failed!
192.168.10.14
--
192.168.10.16
It's so bad! main is failed!
192.168.10.17

5)把/opt/test中所有匹配"main is failed"的行及其前1行的结果打印到/root/result.log中,并加上时间
[root@mq-master02 ~]# echo -e "$(date)\n$(cat /opt/test|grep "main is failed" -B1)"> /root/result.log 
[root@mq-master02 ~]# cat /root/result.log
Wed Oct 10 20:34:15 CST 2018
192.168.10.13
It's so bad! main is failed!
--
192.168.10.16
It's so bad! main is failed!

[root@mq-master02 ~]# echo -e "$(date)\n$(cat /opt/test|grep "main is failed" -B1) \n" >> /root/result.log
[root@mq-master02 ~]# echo -e "$(date)\n$(cat /opt/test|grep "main is failed" -B1) \n" >> /root/result.log
[root@mq-master02 ~]# echo -e "$(date)\n$(cat /opt/test|grep "main is failed" -B1) \n" >> /root/result.log
[root@mq-master02 ~]# cat /root/result.log
Wed Oct 10 20:34:15 CST 2018
192.168.10.13
It's so bad! main is failed!
--
192.168.10.16
It's so bad! main is failed!
Wed Oct 10 20:35:27 CST 2018
192.168.10.13
It's so bad! main is failed!
--
192.168.10.16
It's so bad! main is failed! 

Wed Oct 10 20:35:29 CST 2018
192.168.10.13
It's so bad! main is failed!
--
192.168.10.16
It's so bad! main is failed! 

Wed Oct 10 20:35:29 CST 2018
192.168.10.13
It's so bad! main is failed!
--
192.168.10.16
It's so bad! main is failed! 

[root@mq-master02 ~]# echo -e "$(date +%Y年%m月%d日) $(date +%A) $(date +%H时%M分%S秒)\n$(echo "今天是个好日子啊") \n" > /root/result.log
You have new mail in /var/spool/mail/root
[root@mq-master02 ~]# echo -e "$(date +%Y年%m月%d日) $(date +%A) $(date +%H时%M分%S秒)\n$(echo "今天是个好日子啊") \n" >> /root/result.log
[root@mq-master02 ~]# echo -e "$(date +%Y年%m月%d日) $(date +%A) $(date +%H时%M分%S秒)\n$(echo "今天是个好日子啊") \n" >> /root/result.log
[root@mq-master02 ~]# cat /root/result.log
2018年10月10日 Wednesday 20时36分49秒
今天是个好日子啊 

2018年10月10日 Wednesday 20时36分52秒
今天是个好日子啊 

2018年10月10日 Wednesday 20时36分54秒
今天是个好日子啊 

2)示例二

ip列表文件
[root@shell002 ~]# cat /opt/ip.list
192.168.10.11  
192.168.10.12    
192.168.10.13    
192.168.10.14  
192.168.10.15
192.168.10.16 
192.168.10.17


main进程状态的检查脚本:
[root@shell002 ~]# cat /opt/script/6_main_check.sh 
#!/bin/bash

for i in $(cat /opt/ip.list)
do
/usr/bin/rsync -e "ssh -p22" -avpgolr /usr/bin/main_check $i:/usr/bin/ > /dev/null 2>&1
ssh -p22 root@$i "echo $i;sh /usr/bin/main_check"
done

[root@shell002 ~]# cat /usr/bin/main_check 
#!/bin/bash
NUM=$(ps -ef|grep -w main|grep -v grep|wc -l)
if [ $NUM -eq 0 ];then
   echo "Oh!My God! It's broken! main is stoped!"
else
   echo "Don't worry! main is running!"
fi


检查脚本执行结果
[root@shell002 ~]# sh /opt/script/6_main_check.sh
192.168.10.11
Don't worry! main is running!
192.168.10.12
Don't worry! main is running!
192.168.10.13
Don't worry! main is running!
192.168.10.14
Don't worry! main is running!
192.168.10.15
Don't worry! main is running!
192.168.10.16
Don't worry! main is running!
192.168.10.17
Don't worry! main is running!

检查脚本执行结果的打印脚本
[root@shell002 ~]# cat /mnt/main_check_result.sh
#!/bin/bash
NUM=$(/bin/bash /opt/script/6_main_check.sh |grep -w "main is stoped"|wc -l)
CONTENT=$(/bin/bash /opt/script/6_main_check.sh |grep -w "main is stoped")
if [ $NUM -ne 0 ];then
    echo -e "$(date +%Y年%m月%d日) $(date +%A) $(date +%H时%M分%S秒)\n$(/bin/bash /opt/script/6_main_check.sh |grep  "main is stoped" -B1)\n">> /mnt/main_check_result.log
else
    echo -e "$(date +%Y年%m月%d日) $(date +%A) $(date +%H时%M分%S秒)\n$(echo "当前时段所有机器的main进程运行正常,无需担心哈!")\n">> /mnt/main_check_result.log
fi

main检查的结果文件内容
[root@shell002 ~]# cat /mnt/main_check_result.log 
2018年10月10日 星期三 20时30分41秒
当前时段所有机器的main进程运行正常,无需担心哈!

2018年10月10日 Wednesday 20时30分46秒
当前时段所有机器的main进程运行正常,无需担心哈!

2018年10月10日 Wednesday 20时35分45秒
当前时段所有机器的main进程运行正常,无需担心哈!

2018年10月10日 Wednesday 20时40分45秒
当前时段所有机器的main进程运行正常,无需担心哈!

猜你喜欢

转载自www.cnblogs.com/kevingrace/p/9768897.html