Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot fetch a value with awk inside a block

Tags:

linux

awk

I'm trying to fetch the value of Layout with gawk inside blocks containing /virtual\s*disk\s*[0-9]+/ that look like this :

# omreport storage vdisk controller=0 vdisk=16
Virtual Disk 16 on Controller PERC H730 Mini (Embedded)

Controller PERC H730 Mini (Embedded)
ID                                : 16
Status                            : Ok
Name                              : Virtual Disk 16
State                             : Ready
Hot Spare Policy violated         : Not Applicable
Encrypted                         : No
Layout                            : RAID-0
Size                              : 3,725.50 GB (4000225165312 bytes)
T10 Protection Information Status : No
Associated Fluid Cache State      : Not Applicable
Device Name                       : /dev/sdp
Bus Protocol                      : SAS
Media                             : HDD
Read Policy                       : Read Ahead
Write Policy                      : Force Write Back
Cache Policy                      : Not Applicable
Stripe Element Size               : 64 KB
Disk Cache Policy                 : Disabled

#

I type the following gawk command to the Layout of the last Virtual Disk :

# omreport storage vdisk controller=0 | gawk 'BEGIN{IGNORECASE=1}/virtual\s*disk\s*[0-9]+/{inBlock=1}! /virtual\s*disk\s*[0-9]+/{inBlock=0}inBlock == 1 && /Layout/{value=$NF}END{print value}'
#  

I expect to see for the example above :

RAID-0

The /Layout/ regexp is always inside the omreport storage vdisk controller=0 blocks and omreport returns one or blocks.

In the case of multiple blocks, I need the Layout value of the last Virtual Disk name matching /virtual\s*disk\s*[0-9]+/.

EDIT0 : Here is a block containing a Virtual Disk name NOT matching /virtual\s*disk\s*[0-9]+/ :

# omreport storage vdisk controller=0 vdisk=0
Virtual Disk 0 on Controller PERC H730 Mini (Embedded)

Controller PERC H730 Mini (Embedded)
ID                                : 0
Status                            : Ok
Name                              : SYSTEM
State                             : Ready
Hot Spare Policy violated         : Not Assigned
Encrypted                         : No
Layout                            : RAID-1
Size                              : 465.25 GB (499558383616 bytes)
T10 Protection Information Status : No
Associated Fluid Cache State      : Not Applicable
Device Name                       : /dev/sda
Bus Protocol                      : SATA
Media                             : HDD
Read Policy                       : Read Ahead
Write Policy                      : Write Back
Cache Policy                      : Not Applicable
Stripe Element Size               : 64 KB
Disk Cache Policy                 : Disabled

#
like image 272
SebMa Avatar asked Nov 06 '25 23:11

SebMa


2 Answers

You may use this gnu awk solution:

omreport storage vdisk controller=0 |
awk -F '\\s*:\\s**' '
BEGIN{IGNORECASE=1}
/virtual\s*disk\s*[0-9]+/ {inBlock=1}
inBlock && $1 == "Layout" {print $2; exit}
'

RAID-0

If you don't have gnu awk then IGNORECASE flag won't work and you should use this solution:

awk -F '[[:blank:]]*:[[:blank:]]*' '
/Virtual[[:blank:]]*Disk[[:blank:]]*[0-9]+/ {inBlock=1}
inBlock && $1 ~ /Layout/ {print $2; exit}'
like image 76
anubhava Avatar answered Nov 09 '25 22:11

anubhava


Using regex in GNU awk with your shown samples please try following awk code.

awk -v RS= '
match($0,/: Virtual Disk [0-9]+\n.*Layout[^:]+:[[:space:]]+([^\n]+)/,arr){
  print arr[1]
}
'  Input_file
like image 26
RavinderSingh13 Avatar answered Nov 09 '25 22:11

RavinderSingh13