Shell遍历文件,对每行进行正则匹配

Shell查看文件的最后5行,并对每行进行正则匹配,代码如下:

#!/bin/sh



pattern="HeartBeat"

tail -n 5 /home/test/log/log_20150205.log | while read line

do

    if [[ $line =~ $pattern ]]

    then

        echo "Match"

    else

        echo "No match"

    fi    

    echo $line

done

 

你可能感兴趣的:(shell)