Solved

using awk to return complete stanza for matched fields

Posted on 2008-06-19
8
332 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
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…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

920 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

13 Experts available now in Live!

Get 1:1 Help Now