For Programmers: Free Programming Magazines  


Home > Archive > AWK > April 2007 > extracting data









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author extracting data
Si...

2007-02-26, 7:57 am

Hi, I'm new to awk and can't find in the manuals how to extract
results from a regular expression.

for example the following lines contain bid/ask values .Bid or Ask
maybe missing. ... indicate other data on the line so Bid/Ask are not
going to always be in the same column.

.... Bid '1.234' Ask '1.238' ...
.... Ask '12.47' ...

I can use regular expressions to find the values I'm interested in but
how do I extract the actual values?

Vassilis

2007-02-26, 7:57 am


=CF/=C7 Si... =DD=E3=F1=E1=F8=E5:
> Hi, I'm new to awk and can't find in the manuals how to extract
> results from a regular expression.
>
> for example the following lines contain bid/ask values .Bid or Ask
> maybe missing. ... indicate other data on the line so Bid/Ask are not
> going to always be in the same column.
>
> ... Bid '1.234' Ask '1.238' ...
> ... Ask '12.47' ...
>
> I can use regular expressions to find the values I'm interested in but
> how do I extract the actual values?


Hi,
maybe you don't need regular expressions. A simple string comparison
may do the trick. I will only outline the algorithm here, because your
input doesn't seem clear enough.
Iterate though fields in a record and check if any field is "Bid" or
"Ask". Then, next field is the wanted value.
If you still have questions, post a small input and desired output.
You should also get yourself familiarised with Effective AWK
Programming (found here: http://tinyurl.com/zmaxz)

Ed Morton

2007-02-26, 7:57 am

Si... wrote:
> Hi, I'm new to awk and can't find in the manuals how to extract
> results from a regular expression.
>
> for example the following lines contain bid/ask values .Bid or Ask
> maybe missing. ... indicate other data on the line so Bid/Ask are not
> going to always be in the same column.
>
> ... Bid '1.234' Ask '1.238' ...
> ... Ask '12.47' ...
>
> I can use regular expressions to find the values I'm interested in but
> how do I extract the actual values?
>


This extracts the string matching the RE "a|b":

awk -v re="a|b" '
function extract(str,regexp)
{ RMATCH = (match(str,regexp) ? substr(str,RSTART,RLENGTH) : "")
return RSTART
}
extract($0,re) { print RMATCH }
'

Regards,

Ed.
Si...

2007-02-26, 7:57 am

thanks guys

Si...

Basjosterten70

2007-04-02, 5:43 am

Lindsay Lohan Doing A Hung Guy!
http://Lindsay-Lohan-Doing-A-Hung-G...hp?movie=148803
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com