LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Lynx + Grep + Variable... (https://www.linuxquestions.org/questions/linux-general-1/lynx-grep-variable-868286/)

thecurious1 03-13-2011 08:44 AM

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 ^_^

corp769 03-13-2011 09:56 AM

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

druuna 03-13-2011 10:00 AM

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.

David the H. 03-13-2011 10:02 AM

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.

thecurious1 03-13-2011 10:17 AM

Thanks a lot guys, this is exactly what i needed. taken note of everything here.

Appreciate the help ^_^ !

David the H. 03-13-2011 10:24 AM

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. :)

druuna 03-13-2011 10:29 AM

@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.

corp769 03-13-2011 10:32 AM

Quote:

Originally Posted by druuna (Post 4289131)
@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...

druuna 03-13-2011 10:34 AM

@corp769: man sed ;) ;)

Quote:

-r, --regexp-extended

use extended regular expressions in the script.

David the H. 03-13-2011 10:49 AM

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).

corp769 03-13-2011 10:50 AM

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.