-   Linux - Newbie (
-   -   Need to extract certain values from kudzu-output. (

MheAd 07-01-2008 04:38 PM

Need to extract certain values from kudzu-output.
Hello boys 'n girls.

I'm writing this script that will enable simplified usage of cdrdao burning application.

I want to use kudzu (with -p -c cdrom options) to identify the bus,id and lun of my drive. I know I can use even cdrdao command with scanbus-option, but that requires me to run it as root (at least in Fedora), which is not desired solution. So anyway, to make the story short (I'll not go into the details regarding the script itself), let me show you how I did it.

By writing:


kudzu -p -c cdrom

I get following output:

class: CDROM
bus: SCSI
detached: 0
device: scd0
desc: "_NEC DVD_RW ND-3500AG"
host: 4
id: 0
channel: 0
lun: 0

Now, from this output I need to extract values from host, id and lun lines. I only need numbers and the output should read host,id,lun - on single line, which in this case would be 4,0,0. That, I will use as the value of the variable in my script. Anyway, I'm pretty sure there is much nicer way of extracting these values, but here's how I did it. I wrote a bunch of piped commands. It works but looks clumsy.


kudzu -p -c cdrom | grep host --after-context 3 | grep -v channel | sed 's/[^0-9]//g' | tr '\n' ',' | sed 's/,$//'

So I basically probe here for class CDROM.
From the output, I grep for the host (since from this line the things are getting interesting to me) + 3 lines bellow (so I get all numbers).
Then I grep out line 'channel' since I don't need it.
Then, I remove all non-nummeric characters from the remaining lines so I only have numbers left.
Then I translate new-line characters to commas, to put the numbers together and have them on the same line seperated by commas.
Finaly I delete the last ','.
The result is the desired 4,0,0 - extracted from host:, id: and lun: fields.

Do you guys have a better suggestion to "surgically" remove the stuff above and only have these values left as output, formated as above?

Please only bash-stuff, no Perl and similar things since I have zero knowledge.

Thanks in advance!

jailbait 07-01-2008 05:16 PM

You might be able to get what you want easier by starting with this command:

cdrecord -scanbus

Steve Stites

harry edwards 07-01-2008 05:27 PM

This amendment strips out one of your pipe commands:


kudzu -p -c cdrom  |egrep "host|channel|lun" | sed 's/[^0-9]//g' | tr '\n' ',' | sed 's/,$//'

MheAd 07-02-2008 05:02 AM


Originally Posted by harry edwards (Post 3200743)
This amendment strips out one of your pipe commands:


kudzu -p -c cdrom  |egrep "host|channel|lun" | sed 's/[^0-9]//g' | tr '\n' ',' | sed 's/,$//'

That one looks even nicer. That's what I'm talking about, yes. Thanks. Although, one correction is that you should put the host|id|lun, instead of channel. But nevermind. A typo. The principle is still correct.

I wonder though whether there is a nice awk string that could accomplish this too. Hmm...

All times are GMT -5. The time now is 10:06 AM.