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 ^_^ |
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 }' Josh |
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') |
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-')" 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#* * }" |
Thanks a lot guys, this is exactly what i needed. taken note of everything here.
Appreciate the help ^_^ ! |
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') Code:
version=$(lynx -dump http://www.safer-networking.org/en/download/ | egrep -o '[0-9]{4}-[0-9]{2}-[0-9]{2}' |
@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. |
Quote:
|
@corp769: man sed ;) ;)
Quote:
|
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). |
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....
|
All times are GMT -5. The time now is 12:49 AM. |