awk 没有产生所需的输出

awk 没有产生所需的输出

下面是我的命令输出。当我通过 处理它时awk,它给出了不需要的输出。我究竟做错了什么?

# 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.......

我只期待看到“LV 名称”和“物理卷”。这意味着一个条目只能显示一次。我期望的输出如下:

/dev/Appsvg/apps01 /dev/emcpoweraq
/dev/Appsvg/apps02 /dev/emcpoweraq
/dev/Appsvg/apps03 /dev/emcpoweraq

答案1

如果我猜对了,您只需要一组额外的花括号来组合最后两个语句(p=$3print l,p):

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

对于 Ulrich Schwarz 的评论,更明显的可能是:

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

您问题中的命令awk按预期分配“l”和“p”,但“print l,p”之前没有条件,因此它在每一行上执行。

相关内容