Using Grep/Awk/Sed to get a substring from a command
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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:
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.