test.yml:
ID : id
mySer:
- name : xyz
type : typeX
myServ2:
- name : wyz
params:
- name :ppp
myExample:
- name : levi
type : typeX
test.sh:
#!/bin/bash
str=`cat test.yml`;
function match {
MATCHES=()
local mstr=$1 re="($2)(.*)"
while [[ -n $mstr && $mstr =~ $re ]]; do
MATCHES+=("${BASH_REMATCH[1]}")
mstr=${BASH_REMATCH[2]}
done
}
match "$str" 'name : [a-zA-Z0-9]+\s+type : typeX'
for (( v=0; v < ${#MATCHES[@]}; v++)); do
echo ":: ${MATCHES[$v]}";
done
The output:
:: name : xyz
type : typeX
:: name : levi
type : typeX
However, if you only want to see the name lines, you can pipe that into grep ( | grep name ) where you only show name and then the output would look like this:
:: name : xyz
:: name : levi