dimanche 3 juillet 2016

Read each lines from an input file and print desired matched patterns

I have the following details captured in a input.txt file from a command. 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".

input.txt:

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

I have the following code written but not seems to be correct please help.

while IFS= read -r line; do
 if [[ $line =~ stopped|aborted|isn't running ]]; then
 echo "$line"
 else
 echo "ALL ARE RUNNING"
 fi
done < "input.txt"

My desired output would be like this, If any matched pattern "Stopped|Aborted|isn't running" in the lines.

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

If no matched pattern in the lines & all are "Running" then print "ALL ARE RUNNING"

Thanks in advance,

Aucun commentaire:

Enregistrer un commentaire