LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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
 
LinkBack Search this Thread
Old 03-13-2011, 08:44 AM   #1
thecurious1
LQ Newbie
 
Registered: Dec 2007
Posts: 2

Rep: Reputation: 0
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.
 
Old 03-13-2011, 09:56 AM   #2
corp769
Guru
 
Registered: Apr 2005
Posts: 5,798

Rep: Reputation: 978Reputation: 978Reputation: 978Reputation: 978Reputation: 978Reputation: 978Reputation: 978Reputation: 978
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
 
Old 03-13-2011, 10:00 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
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.
 
Old 03-13-2011, 10:02 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946
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.
 
Old 03-13-2011, 10:17 AM   #5
thecurious1
LQ Newbie
 
Registered: Dec 2007
Posts: 2

Original Poster
Rep: Reputation: 0
Thanks a lot guys, this is exactly what i needed. taken note of everything here.

Appreciate the help ^_^ !
 
Old 03-13-2011, 10:24 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946
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.
Old 03-13-2011, 10:29 AM   #7
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
@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.
 
Old 03-13-2011, 10:32 AM   #8
corp769
Guru
 
Registered: Apr 2005
Posts: 5,798

Rep: Reputation: 978Reputation: 978Reputation: 978Reputation: 978Reputation: 978Reputation: 978Reputation: 978Reputation: 978
Quote:
Originally Posted by druuna View Post
@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...
 
Old 03-13-2011, 10:34 AM   #9
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
@corp769: man sed

Quote:
-r, --regexp-extended

use extended regular expressions in the script.
 
Old 03-13-2011, 10:49 AM   #10
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946
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.
 
Old 03-13-2011, 10:50 AM   #11
corp769
Guru
 
Registered: Apr 2005
Posts: 5,798

Rep: Reputation: 978Reputation: 978Reputation: 978Reputation: 978Reputation: 978Reputation: 978Reputation: 978Reputation: 978
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....
 
  


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 On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do you grep a variable? dbrazeau Programming 12 03-11-2010 08:57 PM
Pipe a variable using grep in a script. okos Linux - Software 6 08-17-2009 02:31 AM
Grep variable with space brainlesseinstein Linux - General 3 08-09-2009 11:28 PM
Using a variable containing a filename in grep TrumpetMan258 Programming 2 03-01-2008 01:27 PM
How to use grep to search for a specific variable.. memo007 Linux - Software 4 02-24-2007 05:52 PM


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