[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: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秒 今天是个好日子啊 |