LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 07-01-2008, 04:38 PM   #1
MheAd
Member
 
Registered: Jun 2007
Distribution: Ubuntu 11.04
Posts: 186

Rep: Reputation: 36
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:

Code:
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.


Code:
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!
 
Old 07-01-2008, 05:16 PM   #2
jailbait
Guru
 
Registered: Feb 2003
Location: Blue Ridge Mountain
Distribution: Debian Wheezy, Debian Jessie
Posts: 7,548

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

cdrecord -scanbus

------------------
Steve Stites
 
Old 07-01-2008, 05:27 PM   #3
harry edwards
Member
 
Registered: Nov 2007
Location: Lincolnshire, UK
Distribution: CentOS, Fedora, and Suse
Posts: 365

Rep: Reputation: 48
This amendment strips out one of your pipe commands:

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

Last edited by harry edwards; 07-01-2008 at 05:28 PM.
 
Old 07-02-2008, 05:02 AM   #4
MheAd
Member
 
Registered: Jun 2007
Distribution: Ubuntu 11.04
Posts: 186

Original Poster
Rep: Reputation: 36
Quote:
Originally Posted by harry edwards View Post
This amendment strips out one of your pipe commands:

Code:
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...

Last edited by MheAd; 07-02-2008 at 05:09 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
help for kudzu/kudzu.h alrawab Programming 5 12-24-2004 11:31 AM
kudzu allelopath Linux - General 1 06-09-2003 11:53 AM
Kudzu j0rg3 Linux - Newbie 1 04-20-2003 03:01 PM
Help with kudzu! fl0ppy Linux - Software 1 11-12-2002 10:06 AM
Kudzu LinuxQuest01 Linux - Software 2 07-23-2002 10:13 AM


All times are GMT -5. The time now is 01:05 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration