LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-12-2010, 07:19 PM   #1
johnjust
LQ Newbie
 
Registered: Aug 2009
Distribution: Ubuntu 9.10 - Karmic Koala
Posts: 4

Rep: Reputation: 0
Using Grep/Awk/Sed to get a substring from a command


Hello all, I'm looking for a way to get a substring from the command to identify my NVIDIA Driver Version. I'll be using this in Conky, in case anyone is interested...

The command to identify the driver version is:
Code:
cat /proc/driver/nvidia/version
It returns:
Code:
NVRM version: NVIDIA UNIX x86 Kernel Module  190.53  Tue Dec  8 18:51:41 PST 2009
GCC version:  gcc version 4.4.1 (Ubuntu 4.4.1-4ubuntu8)
I'd like to just grab the "190.53" part of that. There are 2 spaces before and after it - the first 2 occurrences of double spaces, which I figure could be used with a substring command (if there's a better or easier way to do it, please let me know, this is just the solution that jumped out at me).

I know I could get it with some trial and error (maybe a couple of days, if I'm lucky, lol), but can anyone out there get it quicker? I checked out a couple forum posts on the subject, but I'm just getting a lot of errors when I try to adapt the code to fit my needs.

Thanks for any help!
 
Old 01-12-2010, 07:38 PM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,144

Rep: Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124
If it's always in the same field, awk
Code:
awk '/NVIDIA/ {print $8}'
If there is the possibility the text could change, parse for it
Code:
sed -nr '/NVIDIA/ s/.*([[:digit:]]*\.[[:digit:]]*).*/\1/p'
(all untested)
 
Old 01-12-2010, 07:44 PM   #3
johnjust
LQ Newbie
 
Registered: Aug 2009
Distribution: Ubuntu 9.10 - Karmic Koala
Posts: 4

Original Poster
Rep: Reputation: 0
Wow, I wasn't even close... I guess taking other people's examples and trying to change them doesn't always work.

In any case, AWK worked perfectly!

SED only returned ".53"

I assume it's always in the same spot, so I'll stick with the AWK command for now. I'll also have to devote some time to decipher those commands so I actually learn something...

Thanks a bunch! In case anyone wants to use it, here is the full command:

Code:
cat /proc/driver/nvidia/version | awk '/NVIDIA/ {print $8}'
And, in Conky:

Code:
${exec cat /proc/driver/nvidia/version | awk '/NVIDIA/ {print $8}'}

Last edited by johnjust; 01-12-2010 at 07:51 PM.
 
Old 01-12-2010, 07:51 PM   #4
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
Quote:
Originally Posted by johnjust View Post
Wow, I wasn't even close... I guess taking other people's examples and trying to change them doesn't always work.
Correct, it doesn't always work exactly as you want (and sometimes not at all) but it DOES teach you something (whether it works or not, you still learn from it). And taking such commands and playing with them in a console, making small adjustments and re-trying it over and over, can learn you a lot more than just taking a command, finding it works, and using it and forgetting about it.

Cheers!
Sasha

PS - I see you added that line for conky. I'm not familiar with conky, but are you sure you need that "exec" in there? (maybe you do, I'm just asking..)

Last edited by GrapefruiTgirl; 01-12-2010 at 07:53 PM.
 
Old 01-12-2010, 07:55 PM   #5
johnjust
LQ Newbie
 
Registered: Aug 2009
Distribution: Ubuntu 9.10 - Karmic Koala
Posts: 4

Original Poster
Rep: Reputation: 0
Ha, I also added a line in there saying I should learn something from it, which I fully intend to do. My first guess on the AWK command is the "$8" being the 8th "word" in the output, which could be why it would only work if it always appears in the same place. I'm not sure what the "/NVIDIA/" part does - maybe to identify the line?

As far as I know, "exec" is used to execute a shell command inside Conky, although I'm new to that as well.

Last edited by johnjust; 01-12-2010 at 07:59 PM.
 
Old 01-12-2010, 08:02 PM   #6
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,144

Rep: Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124
My bad - told you it was untested ...
Hint - the regex needs to be stricter; adding a class for white-space in the appropriate spot will do it.
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash - awk, sed, grep, ... advice schneidz Programming 13 08-25-2008 09:30 AM
Sed, Awk, grep,Search,delete joyds219 Linux - Newbie 6 04-03-2008 06:15 AM
awk/sed to grep the text ahpin Linux - Software 3 10-17-2007 12:34 AM
diffrence between grep, sed, awk and egrep Fond_of_Opensource Linux - Newbie 3 08-18-2006 08:15 AM
How can I awk/sed/grep the IPs from the maillog? abefroman Programming 7 03-09-2006 10:22 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:57 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