mardi 5 juillet 2016

Read each line to match a pattern

I have the following details in a input.txt file. My requirement is to match the patterns "Stopped|Aborted|isn't running" in any of the lines, need to print that line. And if the all are in "Running" state need to print a single message that "ALL ARE RUNNING".

SVRSVC1 SVRSVC1 NAME SVRSVC1 PID Running SVRSVC2 SVRSVC2 NAME SVRSVC2 PID Running SVRSVC3 SVRSVC3 NAME SVRSVC3 PID Running SVRSVC4 SVRSVC4 NAME SVRSVC4 PID Running SVRSVC5 SVRSVC5 NAME SVRSVC5 PID isn't running SVRSVC6 SVRSVC6 NAME SVRSVC6 PID Running SVRSVC7 SVRSVC7 NAME SVRSVC7 PID Running SVRSVC8 SVRSVC8 NAME SVRSVC8 PID Aborted SVRSVC9 SVRSVC9 NAME SVRSVC9 PID Running SVRSVC10 SVRSVC10 NAME SVRSVC10 PID Running SVRSVC11 SVRSVC11 NAME SVRSVC11 PID Running SVRSVC12 SVRSVC12 NAME SVRSVC12 PID Stopped SVRSVC13 SVRSVC13 NAME SVRSVC13 PID Running SVRSVC14 SVRSVC14 NAME SVRSVC14 PID Running

Here is code i have written but i would like to know if this is the right approach or any easiest way for this. ?

#set -x
RUNTIME=`date +%Y%m%d_%H%M%S`
TOTAL_RUNSTAT=$(more input.txt | wc -l)
while read -r line; do
if [[ $line =~ Stopped|Aborted|"isn't running" ]]; then
 echo $line;
 elif [[ $line =~ Running ]]; then
  echo $line >> runstatuslog_$RUNTIME;
    if [[ `more runstatuslog_$RUNTIME | wc -l` =~ $TOTAL_RUNSTAT ]]; then
         echo "ALL SERVICES RUNNING";
    fi
fi
done < input.txt

Here is the expected output:

SVRSVC5 SVRSVC5 NAME SVRSVC5 PID isn't running SVRSVC8 SVRSVC8 NAME SVRSVC8 PID Aborted SVRSVC12 SVRSVC12 NAME SVRSVC12 PID Stopped

Aucun commentaire:

Enregistrer un commentaire