Awk command for search a string in lspv

I 've got this retourn when i tape the commande lspv:

hdisk0 00c3fce454950416 rootvg active
hdisk1 00c3fce454950416 rootvg active

I want to verify if hdisk0 et hdisk1 have the same pvid (ex : 00c3fce454950416)?
Can you help me with the awk command for exemple.

thanks :smiley:

Hi khalidou13,
I'm not good with awk, but the following script using sed works fine.

result=$(lspv)
pvid0=$(sed -nr 's/hdisk0 ([[:alnum:]]+) .*/\1/p' <<< "$result")
pvid1=$(sed -nr 's/hdisk1 ([[:alnum:]]+) .*/\1/p' <<< "$result")
if [[ $pvid0 == $pvid1 ]]; then
    echo "same pvid"
else
    echo "different pvid"
fi

Hope this helps
Santiago

With awk:

lspv| grep rootvg| awk '{a=$2; getline; if(a!=$2){print "different"}else{print "same"}}'

It's good !!
Thank you for your help !!
:smiley:

Is the grep bloc absolutely necessary?

lspv| awk '/rootvg/{a=$2; getline; if(a!=$2){print "different"}else{print "same"}}'

With just adding /rootvg/ to awk you'll get 2 lines as output for every line "lspv" hands over to awk. It's not compact but it works. If you want to use grep not and still only 1 line as output you will have to change the awk part a bit. I was lazy so that's why I was so typing so inefficient :wink:

Depending on the O/P of the lspv command here is another approach:

lspv | awk '/rootvg/{a[$2]++}!(a[$2]%2){print "same"}'

Thank you
But i still have a problem.
I want to know wich hdisk have only one pvid and also display hdisk with two pvid.

hdisk1 00c3fcd4e516183f testvg active
hdisk2 00c3fcd4e516189b testvg active
hdisk3 00c3fcd4e51618ec testvg active
hdisk6 00c3fcd4e51618ec testvg active
hdisk8 00c3fcd4e516183f testvg active

Hey Khalidou13,

Will the following script work for you?

#!/bin/bash
lspv=$(lspv)
for pvid in $(echo "$lspv" | awk '{ print $2 }' | sort -u); do
    echo -n $pvid
    sed -nr "s/^(hdisk[0-9]+)[[:space:]]+$pvid.*/ \1/p" <<< "$lspv" | tr -d '\n'
    echo
done

My output is:

00c3fcd4e516183f hdisk1 hdisk8
00c3fcd4e516189b hdisk2
00c3fcd4e51618ec hdisk3 hdisk6