String processing in shell
Posted on 2006-05-06
How do I process search/replace strings (something like using substr in perl) in a shell script (bash, ksh, etc.). Here's what I want to do:
- Given a string, I want to:
a) find a substring
b) extract the previous 8B in front of it
For example (spaces added only to flag substring and desired match string; there will be no spaces in the "real" string), given the string (hex characters only):
AB 24BDF5ABED8CA734 09C8A78348C87A6B DFE2334ADAB342097A87F6DE7A5B78C678EF876AB
I will be given a substring (this will be known): 09C8A78348C87A6B
and want to extract the previous 8 bytes (16 characters - 24BDF5ABED8CA734) using only shell commands (this is the unknown I wish to find).
Can this be done? I've tried using
expr index "$string" $substring
to no avail... the first of these three expressions shows the most promise, but the "$substring" matches only by character and not the entire substring - e.g. 0 or 9 or C or 8 or A ...etc. will match. I can do this in Perl, but my environment requires I only use shell.