Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 359
  • Last Modified:

using awk to return complete stanza for matched fields

Hi All,

I am trying to locate a udi reference for the storage capability in Centos 4.6. The hal-find-by-capability tool has not been implemented on this version of Centos.

I need to use the lshal comand to list all the devices on the system and where info.category matches 'storage' I want to find out the udi reference, an axample of the lshal output is below, there are loads of entries and they do not all relate to storage.

From the examples below I would want the udi for the  first stanza returned and any other udi entries for other storage categories.

I was thinking of another awk solution here but if it's not the best tool to use I'm happy to find out more.

As always, any assistance greatly appreciated.

Jools

udi = '/org/freedesktop/Hal/devices/block_IC35L060AVV207-0-VNVB02G2C92ZHT'
  storage.policy.should_mount = false  (bool)
  info.udi = '/org/freedesktop/Hal/devices/block_IC35L060AVV207-0-VNVB02G2C92ZHT'  (string)
  storage.requires_eject = false  (bool)
  storage.hotpluggable = false  (bool)
  storage.removable = false  (bool)
  storage.firmware_version = 'V22OA66A'  (string)
  storage.serial = 'VNVB02G2C92ZHT'  (string)
  info.product = 'IC35L060AVV207-0'  (string)
  storage.drive_type = 'disk'  (string)
  block.storage_device = '/org/freedesktop/Hal/devices/block_IC35L060AVV207-0-VNVB02G2C92ZHT'  (string)
  storage.physical_device = '/org/freedesktop/Hal/devices/ide_0_0'  (string)
  storage.vendor = ''  (string)
  storage.model = 'IC35L060AVV207-0'  (string)
  storage.automount_enabled_hint = true  (bool)
  storage.no_partitions_hint = false  (bool)
  storage.media_check_enabled = true  (bool)
  storage.bus = 'ide'  (string)
  block.minor = 0  (0x0)  (int)
  block.major = 3  (0x3)  (int)
  info.capabilities = 'block storage'  (string)
  info.category = 'storage'  (string)
  info.parent = '/org/freedesktop/Hal/devices/ide_0_0'  (string)
  block.device = '/dev/hda'  (string)
  block.is_volume = false  (bool)
  block.have_scanned = false  (bool)
  block.no_partitions = false  (bool)
  linux.sysfs_path_device = '/sys/block/hda'  (string)
  linux.sysfs_path = '/sys/block/hda'  (string)
  info.bus = 'block'  (string)

udi = '/org/freedesktop/Hal/devices/ide_0_0'
  info.udi = '/org/freedesktop/Hal/devices/ide_0_0'  (string)
  info.product = 'IDE device (master)'  (string)
  ide.channel = 0  (0x0)  (int)
  ide.host = 0  (0x0)  (int)
  info.parent = '/org/freedesktop/Hal/devices/ide_host_0'  (string)
  ide.linux.sysfs_path = '/sys/devices/pci0000:00/0000:00:1f.1/ide0/0.0'  (string)
  linux.sysfs_path_device = '/sys/devices/pci0000:00/0000:00:1f.1/ide0/0.0'  (string)
  linux.sysfs_path = '/sys/devices/pci0000:00/0000:00:1f.1/ide0/0.0'  (string)
  info.bus = 'ide'  (string)

0
jools
Asked:
jools
  • 4
  • 4
1 Solution
 
MikeOM_DBACommented:
If ishal.dat is the file with hte information, try this:

awk '/info.udi/{udi=$3} /info.category/{if ($3 ~ /storage/) print udi;} ' ishal.dat

Open in new window

0
 
joolsAuthor Commented:
Well thats a new one.... I learn something every day!

Just a quicky, it doesn't print off the complete stanza which I was going to use to process but all is not lost (I hope).

If the full stanza cannot be printed is it possible to add another match value, say for instance, comparing 'block.device' to (in the example output above) match hda and print the udi if both fields match but ignore it if block.device is set to hdb, for example.

Thx for the quick response.
0
 
MikeOM_DBACommented:
What is full stanza?
You can add any conditions:

awk '
/info.udi/{udi=$3;ic=""} 
/info.category/{ic=$3} 
/block.device/{if ($3 ~ /hda/ && ic != "") print udi;} 
' ishal.dat
 
 

Open in new window

0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
MikeOM_DBACommented:
Oops missed the "storage" test:

awk '
/info.udi/{udi=$3;ic=""} 
/info.category/{if ($3 ~ /storage/) ic=$3} 
/block.device/{if ($3 ~ /hda/ && ic != "") print udi;} 
' ishal.dat
 

Open in new window

0
 
joolsAuthor Commented:
truly marvellous.
Thanks very much

Jools
0
 
joolsAuthor Commented:
The full stanza is the start of the '^udi =' until the blank new line, just before the next '^udi ='.
If you're feeling like coding please feel free.... :-)

Thanks again for the example!

J
0
 
MikeOM_DBACommented:
Try this:

awk '
$0 == "" {if (bd!="") for (i=0;i<=n;++i) print s[i];}
/^udi/{ic="";bd=""; n=0} 
/info.category/{if ($3 ~ /storage/) ic=$3} 
/block.device/{if ($3 ~ /hda/ && ic != "") bd=$3;} 
{s[n]=$0;++n}
' ishal.dat

Open in new window

0
 
joolsAuthor Commented:
top man!

Thanks

J
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now