Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

using awk to return complete stanza for matched fields

Posted on 2008-06-19
8
Medium Priority
?
354 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
LVL 29

Accepted Solution

by:
MikeOM_DBA earned 1000 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

Give Your Engineering Team a Productivity Boost

Learn why container technology is so powerful and how it can provide your team with productivity gains and other benefits.

Question has a verified solution.

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

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
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…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

688 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