0

No sure if I can do this easily but:

I have a file like

VDS|name=vds01|another_parameter
vol=disk01|parameter|dev=/dev/dev/my_dev1|parameter_A=some_value1
vol=disk02|parameter|dev=/dev/dev/my_dev2|parameter_A=some_value2
vol=disk03|parameter|dev=/dev/dev/my_dev3|parameter_A=some_value3
DOMAIN|name=domain01
VDISK|name=disk01|vol=disk01@vds01|parameter_B=some_valueB|parameter_C=some_value
VDISK|name=disk02|vol=disk02@vds01|parameter_B=some_valueB|parameter_C=some_value
DOMAIN|name=domain02
VDISK|name=disk03|vol=disk03@vds01|parameter_B=some_valueB|parameter_C=some_value

Now I want to grep by /dev/dev/my_dev2 and get the volume name and domain name. I'm getting the volname by:

volname=`grep $dsk ldm_ls-o_disk.out|uniq|cut -d\| -f2|cut -d= -f2`

this will give me disk02 but now I'm stuck on how to get the domain name since I can have several VDISK and the number of lines between the VDISK and DOMAIN is variable. Basically I want to grep the volname and "search" back to find the DOMAIN tag and get the name from it.

  • Could you add an example or two of what the expected output would look like? For the DOMAIN lines you could do something like d=$(grep "DOMAIN|name=" | cut -d= -f2) from the way that I understand the question, but I don't think that is what you are asking. – jmunsch Dec 6 '14 at 21:09
  • I want to find the domain associated with a device. Mainly I have a list of devices, I find their volume name (vol=name) and then I want to get the domain they are assigned to. – BitsOfNix Dec 6 '14 at 21:22
1

Assuming the domain is the most recent before the matching VDISK line, I'd use awk:

awk -F '[=|]' -v dev="/dev/dev/my_dev2" '
    $1 == "vol" && $5 == dev {vol = $2}
    $1 == "DOMAIN" {domain = $3}
    $1 == "VDISK" && $3 == vol {print vol, domain; exit}
' file

This outputs

disk02 domain01

To capture those in shell variables use read with a process substitution:

get_domain () {
    awk -F '[=|]' -v dev="$1" '
        $1 == "vol" && $5 == dev {vol = $2}
        $1 == "DOMAIN" {domain = $3}
        $1 == "VDISK" && $3 == vol {print vol, domain; exit}
    ' file
}

read volume domain < <(get_domain "/dev/dev/my_dev2")
  • If you need to query several devices from this file, I'd use something like awk or perl to parse the whole file, and then output the domains for each respective device. – glenn jackman Dec 6 '14 at 21:40
-1

For the moment I'm using this solution, but I would like something cleaner.

volname=`grep $dsk ldm_ls-o_disk.out|uniq|cut -d\| -f2|cut -d= -f2`
linenr=`grep -n $volname ldm_ls-o_disk.out |grep VDISK|cut -d\: -f1`
retval=1
while [ $retval -eq 1 ]
do
        line=`sed -n "${linenr}p" ldm_ls-o_disk.out|grep DOMAIN`
        if [ $? -eq 0 ]; then
                retval=0;
                domain=`echo $line |cut -d= -f2`
        else
                linenr=$(($linenr-1))
        fi
done
  • 1
    This isn't an answer: it should be an edit to your question. And please edit in information requested by jmunsch... – jasonwryan Dec 6 '14 at 21:22
  • this is an answer as it does exactly what I want, although it's slow and I would like something simpler. Doing like this to 200+ devices is just overkill. Although when I posted the question I had only volume name and was thinking on how to obtain the desired result. – BitsOfNix Dec 6 '14 at 21:24
  • 1
    Then you should be posting on Code Review, not here... – jasonwryan Dec 6 '14 at 21:29
  • since when? this is shell scripting question which is part of this site you can find more info -> unix.stackexchange.com/help/on-topic. – BitsOfNix Dec 6 '14 at 21:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.