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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
03-13-2011, 08:44 AM
|
#1
|
|
LQ Newbie
Registered: Dec 2007
Posts: 2
Rep:
|
Lynx + Grep + Variable...
Hi there,
I am TRYING to store the output from the command below as a variable, but i am having no luck and slowly going insane..
ideally, i would like to store the date only [2011-03-09 in this instance] as the variable but have been unable to get it or anything close.
Command [Not working]
lynx -dump http://www.safer-networking.org/en/download/ | version=`grep '2011-'`
echo $version
the output from;
lynx -dump http://www.safer-networking.org/en/download/ | grep '2011-'
is;
[37]Detection updates^© 2011-03-09 - [38]product description
Any help or insight on this would be greatly appreciated.
Thanks in advance.
Also i apologize if this is poorly written or in the wrong place or anything else annoying ^_^
Last edited by thecurious1; 03-13-2011 at 09:40 AM.
|
|
|
|
03-13-2011, 09:56 AM
|
#2
|
|
Guru
Registered: Apr 2005
Location: /dev/null
Distribution: technixOS
Posts: 5,723
|
If the output is really how it comes out when you run that, you can try the following using the awk command to print the third field of text:
Code:
lynx -dump http://www.safer-networking.org/en/download/ | grep '2011-' | awk '{ print $3 }'
I'm not on linux right now, so I can come back to this in about an hour and a half and run it at home if need be. Of course once I do I can modify it to search in a better way.
Josh
|
|
|
|
03-13-2011, 10:00 AM
|
#3
|
|
LQ Veteran
Registered: Sep 2003
Location: the Netherlands
Distribution: lfs, debian, rhel
Posts: 8,868
|
Hi,
Another option, only using lynx and sed and probably output layout independent:
Code:
version=$(lynx -dump http://www.safer-networking.org/en/download/ | sed -n '/2011-/s/.*\([0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}\).*/\1/p')
Hope this helps.
|
|
|
|
03-13-2011, 10:02 AM
|
#4
|
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,697
|
First of all, please use [code][/code] tags around your code, to preserve formatting and to improve readability.
You can't capture just the last command in a pipe chain. Each section of the pipe executes in its own subshell, so whatever variables you set there are lost when the command terminates.
Not to mention that you can't simultaneously set a variable and pipe something into the command that's setting it.
Instead you have to capture the chain as a whole.
Code:
version="$(lynx -dump http://www.safer-networking.org/en/download/ | grep '2011-')"
echo "$version"
Note that $(..) is recommended over `..` when using embedded commands.
Edit: based on the OP's string, once you have it in a variable, you could do something like this with parameter substitution:
Code:
version="${version#* * }"
version="${version%% *}"
echo "$version"
I can't guarantee how robust it is though, as it depends on that exact pattern of text.
Last edited by David the H.; 03-13-2011 at 10:12 AM.
Reason: Revised post based on re-reading the OP.
|
|
|
|
03-13-2011, 10:17 AM
|
#5
|
|
LQ Newbie
Registered: Dec 2007
Posts: 2
Original Poster
Rep:
|
Thanks a lot guys, this is exactly what i needed. taken note of everything here.
Appreciate the help ^_^ !
|
|
|
|
03-13-2011, 10:24 AM
|
#6
|
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,697
|
Druuna, you can make your sed expression cleaner by using the -r option, to avoid having to use all those backslash escapes:
Code:
version=$(lynx -dump http://www.safer-networking.org/en/download/ | sed -rn '/2011-/s/.*([0-9]{4}-[0-9]{2}-[0-9]{2}).*/\1/p')
But using egrep is even easier.
Code:
version=$(lynx -dump http://www.safer-networking.org/en/download/ | egrep -o '[0-9]{4}-[0-9]{2}-[0-9]{2}'
I'm sure you could make it even more robust by breaking it down into only those individual digits used in dates, but whatever. 
Last edited by David the H.; 03-13-2011 at 10:28 AM.
Reason: deleted unnecessary parentheses
|
|
|
1 members found this post helpful.
|
03-13-2011, 10:29 AM
|
#7
|
|
LQ Veteran
Registered: Sep 2003
Location: the Netherlands
Distribution: lfs, debian, rhel
Posts: 8,868
|
@David the H.: I need to remember the -r option, I always seem to take the "escape road"
The egrep did cross my mind, but even though the example given doesn't need it, I kept the option open to search for a different string and still print the date.
Also: The regex could indeed made cleaner. It now allows 'dates' for 0000-00-00 to 9999-99-99.
|
|
|
|
03-13-2011, 10:32 AM
|
#8
|
|
Guru
Registered: Apr 2005
Location: /dev/null
Distribution: technixOS
Posts: 5,723
|
Quote:
Originally Posted by druuna
@David the H.: I need to remember the -r option, I always seem to take the "escape road"
The egrep did cross my mind, but even though the example given doesn't need it, I kept the option open to search for a different string and still print the date.
Also: The regex could indeed made cleaner. It now allows 'dates' for 0000-00-00 to 9999-99-99.
|
What is the -r option used for? I never used it before...
|
|
|
|
03-13-2011, 10:34 AM
|
#9
|
|
LQ Veteran
Registered: Sep 2003
Location: the Netherlands
Distribution: lfs, debian, rhel
Posts: 8,868
|
@corp769: man sed
Quote:
-r, --regexp-extended
use extended regular expressions in the script.
|
|
|
|
|
03-13-2011, 10:49 AM
|
#10
|
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,697
|
With sed and grep, the default parsing uses only a small subset of the regex special characters (basically you only get * and [], IIRC), and backslash escaping actually enables the rest. When -r is used, the situation is reversed, and all special regex characters are enabled by default, and disabled when backslashed.
The grep man page explains it all fairly well.
Edit: It's sed -r, and grep -E (a.k.a egrep).
Last edited by David the H.; 03-13-2011 at 11:14 AM.
|
|
|
|
03-13-2011, 10:50 AM
|
#11
|
|
Guru
Registered: Apr 2005
Location: /dev/null
Distribution: technixOS
Posts: 5,723
|
I'm on windows right now, and the network is iffy about some sites. I'm lucky I can get on LQ in the first place....
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 11:54 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|