Gentoo Archives: gentoo-user

From: Jeff Alge <algej@×××××××××.net>
To: gentoo-user@l.g.o
Subject: Re: [gentoo-user] Bash + Awk + variable assignation.
Date: Tue, 20 Sep 2005 16:35:22
Message-Id: 1127233654.5118.26.camel@gemini
In Reply to: [gentoo-user] Bash + Awk + variable assignation. by Ow Mun Heng
1 On Tue, 2005-09-20 at 13:56 +0800, Ow Mun Heng wrote:
2 > Remember my script that sends out HTML emails? Well, that's done, but
3 > it's in-efficient.
4 >
5 > The actual script that parses screen-scrapes. Initial script only went
6 > to get the "Last Price" now, I would like to add the "Day Range"
7 >
8 > last_price()
9 > {
10 > value="$(lynx -dump "$url$symbol" | grep 'Last price:' | \
11 > awk -F: 'NF > 1 && $(NF) != "N/A" { print $(NF) }' )"
12 > }
13 >
14 > day_range()
15 > {
16 > day_range="$(lynx -dump "$url$symbol" | grep 'Low \& High:' | \
17 > awk -F: 'NF > 1 && $(NF) != "N/A" { print $(NF) }' )"
18 > }
19 >
20 > The above is in-efficient because I need to call the script to get the
21 > page 2 times.
22 >
23 > Doing a
24 >
25 > lynx -dump "$url$symbol" | egrep -i '(Last Price|Low \& High)'| awk -F:
26 > 'NF > 1 && $(NF) != "N/A" { print $(NF) }'
27 >
28 > will work, but then, there will be 2 values associated with it :
29 > 7.35
30 > 7.127 - 7.38
31 >
32 > Can anyone help with a better awk script so that each value will be
33 > associated with each line?
34 >
35 > eg:
36 > last_price=7.35
37 > day_range=7.127 - 7.38
38 >
39 > w/o actually piping the lynx output to a file (actually, that would be
40 > the easy way)
41 >
42 > On the other hand, how does one use awk for multigreps like egrep '(pop|
43 > test)'
44 >
45 > I've tried variation of
46 > 1. awk "/Low & High/"
47 > 2. awk "/Low & High/" && /Last/
48 > 3. awk '{"/Low & High/" && /Last/}'
49 > all of which doesn't work except for No. 1
50 >
51 > --
52 > Ow Mun Heng
53 > Gentoo/Linux on DELL D600 1.4Ghz 1.5GB RAM
54 > 98% Microsoft(tm) Free!!
55 > Neuromancer 13:54:21 up 1 day, 4:11, 6 users, load average: 3.66, 2.92,
56 > 2.71
57 >
58 >
59
60 If I understand your question correctly, you're trying to use a single
61 command substitution to assign multiple variables in your bash script
62 from an awk script. In other words, you want to assign multiple
63 variables in the parent process' environment from a child process.
64 There's no direct way that I know of to do this. However, you could do
65 some bash array assignment/awk trickery to assign multiple values into
66 an array like this:
67
68 declare -a RESULT_A=(`lynx -dump "$url$symbol" | gawk -F: '
69 BEGIN { IGNORECASE=1 ; }
70 /Last Price/{if (NF>1 && $(NF) != "N/A") { last_price=$NF; next;}}
71 /Low & High/{if (NF>1 && $(NF) != "N/A") { day_range=$NF; next;}}
72 END { printf("[0]=\"%s\" [1]=\"%s\"\n",last_price,day_range); }'`)
73 last_price=${RESULT_A[0]}
74 day_range=${RESULT_A[1]}
75
76 The BEGIN block assignment gets rid of your egrep -i statement by moving
77 case insensitivity into awk. The middle two lines assign internal vars
78 based on the regex pattern seen. The END block produces output in the
79 form [0]="something" [1]="something else". This gets substituted into
80 the RESULT_A array assignment, which you can then pick apart from bash.
81
82 HTH
83
84 Jeff
85
86
87 --
88 gentoo-user@g.o mailing list