On Tue, 2005-09-20 at 13:56 +0800, Ow Mun Heng wrote:
> Remember my script that sends out HTML emails? Well, that's done, but
> it's in-efficient.
>
> The actual script that parses screen-scrapes. Initial script only went
> to get the "Last Price" now, I would like to add the "Day Range"
>
> last_price()
> {
> value="$(lynx -dump "$url$symbol" | grep 'Last price:' | \
> awk -F: 'NF > 1 && $(NF) != "N/A" { print $(NF) }' )"
> }
>
> day_range()
> {
> day_range="$(lynx -dump "$url$symbol" | grep 'Low \& High:' | \
> awk -F: 'NF > 1 && $(NF) != "N/A" { print $(NF) }' )"
> }
>
> The above is in-efficient because I need to call the script to get the
> page 2 times.
>
> Doing a
>
> lynx -dump "$url$symbol" | egrep -i '(Last Price|Low \& High)'| awk -F:
> 'NF > 1 && $(NF) != "N/A" { print $(NF) }'
>
> will work, but then, there will be 2 values associated with it :
> 7.35
> 7.127 - 7.38
>
> Can anyone help with a better awk script so that each value will be
> associated with each line?
>
> eg:
> last_price=7.35
> day_range=7.127 - 7.38
>
> w/o actually piping the lynx output to a file (actually, that would be
> the easy way)
>
> On the other hand, how does one use awk for multigreps like egrep '(pop|
> test)'
>
> I've tried variation of
> 1. awk "/Low & High/"
> 2. awk "/Low & High/" && /Last/
> 3. awk '{"/Low & High/" && /Last/}'
> all of which doesn't work except for No. 1
>
> --
> Ow Mun Heng
> Gentoo/Linux on DELL D600 1.4Ghz 1.5GB RAM
> 98% Microsoft(tm) Free!!
> Neuromancer 13:54:21 up 1 day, 4:11, 6 users, load average: 3.66, 2.92,
> 2.71
>
>
If I understand your question correctly, you're trying to use a single
command substitution to assign multiple variables in your bash script
from an awk script. In other words, you want to assign multiple
variables in the parent process' environment from a child process.
There's no direct way that I know of to do this. However, you could do
some bash array assignment/awk trickery to assign multiple values into
an array like this:
declare -a RESULT_A=(`lynx -dump "$url$symbol" | gawk -F: '
BEGIN { IGNORECASE=1 ; }
/Last Price/{if (NF>1 && $(NF) != "N/A") { last_price=$NF; next;}}
/Low & High/{if (NF>1 && $(NF) != "N/A") { day_range=$NF; next;}}
END { printf("[0]=\"%s\" [1]=\"%s\"\n",last_price,day_range); }'`)
last_price=${RESULT_A[0]}
day_range=${RESULT_A[1]}
The BEGIN block assignment gets rid of your egrep -i statement by moving
case insensitivity into awk. The middle two lines assign internal vars
based on the regex pattern seen. The END block produces output in the
form [0]="something" [1]="something else". This gets substituted into
the RESULT_A array assignment, which you can then pick apart from bash.
HTH
Jeff
--
[email protected] mailing list