Solved

using awk to return complete stanza for matched fields

Posted on 2008-06-19
8
337 Views
Last Modified: 2010-05-18
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
Comment
Question by:jools
  • 4
  • 4
8 Comments
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 21822060
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
 
LVL 19

Author Comment

by:jools
ID: 21822285
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
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 21822635
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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 29

Accepted Solution

by:
MikeOM_DBA earned 250 total points
ID: 21822643
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
 
LVL 19

Author Closing Comment

by:jools
ID: 31468753
truly marvellous.
Thanks very much

Jools
0
 
LVL 19

Author Comment

by:jools
ID: 21823364
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
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 21823911
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
 
LVL 19

Author Comment

by:jools
ID: 21824132
top man!

Thanks

J
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Utilizing an array to gracefully append to a list of EmailAddresses
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question