[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: parsing problem



Hi
On Thu, Dec 09, 2010 at 11:33:13AM -0500, Bernard Fay wrote:
> Hi,
> 
> I have long strings to parse which have permanent and variable information.
>  Somewhere in the strings we have something like  ".... Handle: 0x12a7
> ....".  Handle is permanent and 0x12a7 is variable.  I wish to be able to
> extract the variable part (0x12a7) and assign it to a variable.
> 
> Does anyone have an idea how to extract this information?
Beside the perl-proposal: it depends what you want to do then with the
data... You can already do it with sed: The sed-script 
s/.*Handle: \(0x[0-9a-f]*\) .*/\1/
does exactly this. I tell sed:
 - s/..../.../: replace the expression between the first two slashes by
   the one between the second two
 - the ".*" means: arbitrary characters --- so the first and the last
   part are ignored
 - then i NEED the exact expression "Handle: "
 - after that, "0x" followed by an arbitrary long sequence of digits
   and letters a to f. This part ist "combined" (this is done by
   "\(...\)" and referenced as "first variable".
 - then this expression is replaces by this first variable:

Now, if you send your strings to the standard input of sed, it will
write the variable into its output:

echo "bla bla bla Handle: 0x12a7 bla bla bla" | sed -e 's/.*Handle: \(0x[0-9a-f]*\) .*/\1/'

gives: 

0x12a7


HTH,

Axel


Reply to: