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

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:"


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"] 
 <command > $NAME

Thanks in advance

Try as follows:

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

Store in a bash shell variable is simple too:

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

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.

Linux sysadmin blog - Linux/Unix Howtos and Tutorials - Linux bash shell scripting wiki