Solved

using awk to return complete stanza for matched fields

Posted on 2008-06-19
8
326 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
 
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now