LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 09-29-2006, 11:09 AM   #1
davidkline
LQ Newbie
 
Registered: Nov 2005
Location: Chiago, IL
Distribution: VectorLinux
Posts: 26

Rep: Reputation: 15
awk question


I would like to use awk to extract the major device number from the file /proc/devices (first field) in a shell script. Something like this:

#!/bin/sh
number=32

major=$(..awk command using $number..)

I tried a few variations and I can't seem to get it to work. Does anyone know how to do this?

Please advise, thanks in advance,
/david
 
Old 09-29-2006, 11:14 AM   #2
ygloo
Member
 
Registered: Aug 2006
Distribution: slack
Posts: 323

Rep: Reputation: 30
major=$( cat /proc/devices | awk '{ print $1 }' )
 
Old 09-29-2006, 11:24 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Code:
#!/bin/bash

number=128

awk -v NUMBER="$number" '$1 == NUMBER { print $0 }' /proc/devices
If I understand you correctly you want to use a variable, set in bash/sh, that is used in awk. You need to use awk's -v option (-v NUMBER="$number"). NUMBER can be anything you want and is used within awk, the $number part is the bash variable you 'give' to awk.

The above code snippet prints the entire line if there is a match (I don't have a 32, so I substituted it with 128).

Hope this helps.
 
Old 09-29-2006, 12:01 PM   #4
davidkline
LQ Newbie
 
Registered: Nov 2005
Location: Chiago, IL
Distribution: VectorLinux
Posts: 26

Original Poster
Rep: Reputation: 15
Thank you for your responses...

I'm sorry ... I don't think I worded the question correctly. What I'd like to do is to search the file /proc/devices for a specific device name specified by a variable and have awk (or maybe there is something better) return the device number. For example:

#!/bin/sh
device="mydriver"
major=$( ...awk command... )

Thanks in advance,
 
Old 09-29-2006, 12:07 PM   #5
ygloo
Member
 
Registered: Aug 2006
Distribution: slack
Posts: 323

Rep: Reputation: 30
#!/bin/sh
number=32

major=$( cat /proc/devices | grep $number | awk '{ print $1 }' )

Last edited by ygloo; 09-29-2006 at 12:09 PM.
 
Old 09-29-2006, 12:11 PM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

It takes just a few minor changes to my original code posted to do this:
Code:
#!/bin/bash

number="netlink"

awk -v number="$number" '$2 == number { print $1 }' /proc/devices
Sample run (script is called tets):

$ ./tets
36


This: 36 netlink is the line from /proc/devices,

If you don't want to use a script, you can use the following one-liner from the commandline:

awk '/netlink/ { print $1}' /proc/devices

Hope this helps.
 
Old 09-29-2006, 12:14 PM   #7
davidkline
LQ Newbie
 
Registered: Nov 2005
Location: Chiago, IL
Distribution: VectorLinux
Posts: 26

Original Poster
Rep: Reputation: 15
Thank you! This works great...
 
Old 09-29-2006, 12:15 PM   #8
ygloo
Member
 
Registered: Aug 2006
Distribution: slack
Posts: 323

Rep: Reputation: 30
awk '/$number/ { print $1}' /proc/devices

Last edited by ygloo; 09-29-2006 at 12:20 PM.
 
Old 09-29-2006, 12:32 PM   #9
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

@ygloo:

Quote:
Originally Posted by ygloo
awk '/$number/ { print $1}' /proc/devices
This will not work.

AWK does not substitute the content of the variable ($number) and there will be no hit/output:
$ number="netlink"
$ awk '/$number/ { print $1}' /proc/devices
$
 
Old 09-29-2006, 12:37 PM   #10
ygloo
Member
 
Registered: Aug 2006
Distribution: slack
Posts: 323

Rep: Reputation: 30
found a way to substitute variable within awk...
quote with '
awk '/'$number'/ { print $1}' /proc/devices

___________________________found it here:
http://www.shelldorado.com/

Last edited by ygloo; 09-30-2006 at 07:30 PM.
 
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
question about awk perfect_circle Programming 4 07-19-2006 02:34 PM
Awk question blanny Linux - Software 6 05-02-2006 08:03 AM
awk question puishor Programming 2 08-26-2005 09:44 AM
awk question jonlake Linux - Software 2 05-04-2005 03:58 PM
awk question dilberim82 Programming 2 10-01-2003 07:12 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

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

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration