1

Below is my command output. When I process it through awk, it is giving unwanted output. What am I doing wrong?

    # lvdisplay -m
          --- Logical volume ---
          LV Name                /dev/Appsvg/apps01
          VG Name                Appsvg
          LV UUID                TckScf-LXdY-BvU1-NGhQ-5vUQ-KoNz-Uus1Of
          LV Write Access        read/write
          LV Status              available
          # open                 1
          LV Size                15.00 GB
          Current LE             3840
          Segments               1
          Allocation             inherit
          Read ahead sectors     auto
          - currently set to     256
          Block device           253:0

          --- Segments ---
          Logical extent 0 to 3839:
            Type                linear
            Physical volume     /dev/emcpoweraq
            Physical extents    0 to 3839


          --- Logical volume ---
          LV Name                /dev/Appsvg/apps02
          VG Name                Appsvg
          LV UUID                FcMopR-57MH-aTrT-3bq2-wUJZ-blEI-161Ivz
          LV Write Access        read/write
          LV Status              available
          # open                 1
          LV Size                10.00 GB
          Current LE             2560
          Segments               1
          Allocation             inherit
          Read ahead sectors     auto
          - currently set to     256
          Block device           253:1

          --- Segments ---
          Logical extent 0 to 2559:
            Type                linear
            Physical volume     /dev/emcpoweraq
            Physical extents    3840 to 6399


          --- Logical volume ---
          LV Name                /dev/Appsvg/apps03
          VG Name                Appsvg
          LV UUID                Ji4ldh-2ffZ-9qmb-BVaz-rwYd-f9HQ-2imPYG
          LV Write Access        read/write
          LV Status              available
          # open                 1
          LV Size                20.00 GB
          Current LE             5120
          Segments               1
          Allocation             inherit
          Read ahead sectors     auto
          - currently set to     256
          Block device           253:2

          --- Segments ---
          Logical extent 0 to 5119:
            Type                linear
            Physical volume     /dev/emcpoweraq
            Physical extents    6400 to 11519
    # lvdisplay -m|awk '/(LV Name)/{l=$3} /(Physical volume)/{p=$3} {print l,p;}'

    /dev/Appsvg/apps01
    /dev/Appsvg/apps01
    /dev/Appsvg/apps01
    /dev/Appsvg/apps01
    /dev/Appsvg/apps01
    /dev/Appsvg/apps01
    /dev/Appsvg/apps01
    /dev/Appsvg/apps01
    /dev/Appsvg/apps01
    /dev/Appsvg/apps01
    /dev/Appsvg/apps01
    /dev/Appsvg/apps01
    /dev/Appsvg/apps01
    /dev/Appsvg/apps01
    /dev/Appsvg/apps01
    /dev/Appsvg/apps01
    /dev/Appsvg/apps01
    /dev/Appsvg/apps01 /dev/emcpoweraq
    /dev/Appsvg/apps01 /dev/emcpoweraq
    /dev/Appsvg/apps01 /dev/emcpoweraq
    /dev/Appsvg/apps01 /dev/emcpoweraq
    /dev/Appsvg/apps01 /dev/emcpoweraq
    /dev/Appsvg/apps02 /dev/emcpoweraq
    /dev/Appsvg/apps02 /dev/emcpoweraq
    /dev/Appsvg/apps02 /dev/emcpoweraq
    /dev/Appsvg/apps02 /dev/emcpoweraq
    /dev/Appsvg/apps02 /dev/emcpoweraq
    ................. output snipped.......
    enter code here

I am looking forward to see the "LV Name" & "Physical volume" only. This means one entry should be visible only one time. The output I am expecting is as below:

/dev/Appsvg/apps01 /dev/emcpoweraq
/dev/Appsvg/apps02 /dev/emcpoweraq
/dev/Appsvg/apps03 /dev/emcpoweraq
  • 1
    Can you clarify the output that you were expecting (by editing your question to give an example)? – Jeff Schaller Nov 16 '15 at 18:03
  • @JeffSchaller - I need output sorted once for matched stringe while it is listing same output many times...if you will see the output of "lvdisplay -m" it should apear 3 times only as i have edited in my post the desired output .... – Linu Nov 16 '15 at 18:10
6

If I'm guessing correctly, you just need an extra set of curly braces to combine the last two statements (p=$3 and print l,p):

                                                           /-     HERE    -\
                                                          \/               \/
lvdisplay -m | awk '/(LV Name)/{l=$3} /(Physical volume)/{{p=$3} {print l,p;}}'

To Ulrich Schwarz's comment, more obvious may be:

lvdisplay -m | awk '/(LV Name)/{l=$3} /(Physical volume)/{p=$3; print l,p;}'

The awk command from your question was assigning "l" and "p" as expected, but "print l,p" had no condition before it, so it was being executed on every line.

  • 3
    p=$3; print l,p; should do it fine, too. – Ulrich Schwarz Nov 16 '15 at 18:09
  • [ @Jeff - You are correct.. can you explain it more.. # lvdisplay -m |awk '/(LV Name)/{l=$3} /(Physical volume)/{{p=$3} {print l,p;}}' /dev/Appsvg/apps01 /dev/emcpoweraq /dev/Appsvg/apps02 /dev/emcpoweraq /dev/Appsvg/apps03 /dev/emcpoweraq ] – Linu Nov 16 '15 at 18:13
  • @Linu I appended an explanation to my answer. – Jeff Schaller Nov 16 '15 at 18:17
  • 1
    Or even: awk '/LV Name/ {printf "%s%s", $NF, OFS}; /Physical volume/ {print $NF}' – glenn jackman Nov 16 '15 at 19:15

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.