Grep -E output into variables in a loop when using bash shell

grep
bash

#1

I need to grep the output from a command, there will be multiple lines for each expression, and use that information in variables for the rest of my script. The grep -E is the easy part. I read an article about putting a single result into a variable, but this is a little more complicated (at least for me). Here is sample output from the grep.

bppllist -allpolicies -U | grep -E "Policy Name:|Policy Type:"

Outputs:

Policy Name:       AAAAA
  Policy Type:         NBU-Catalog
Policy Name:       BBBBB
  Policy Type:         MS-Windows
Policy Name:       CCCCC
  Policy Type:         MS-SQL-Server

What I need to do is capture the Name and Type from each iteration to use for further actions. Some sort of a for or while loop I would guess. Would it be easier to output the grep results to a file then read it in?? I’m open to any options.

For this example, I would want to look at the Type first, and if it meets my criteria, run a command for Name

if  [$TYPE = "MS-Windows"] 
then
 <command > $NAME
fi

Thanks in advance
John


#2

Try as follows:

bppllist -allpolicies -U | grep -E "Policy Name:|Policy Type:" | while IFS=: read -r f1 f2 
 echo "$f1"
 echo "$2"
done

Store in a bash shell variable is simple too:

bppllist -allpolicies -U | grep -E "Policy Name:|Policy Type:" | while IFS=: read -r f1 f2 
 _TYPE="$f1"
 _VALUE="$2"
 # now do something on $_TYPE or $_VALUE
 if [ "${_TYPE}" == "MS-Windows"]
 then
    /path/to/command "${_VALUE}"
 fi
done

#3

I think I have a good base for what I need to do now. I took a combination of your suggestions and something of my own. More work to do, but that can be tomorrow. Thanks for your assistance.