I have a database containing a list of unsigned longs representing a bunch of memory addresses followed by an offset within two brackets (e.g. Mem[offset]
, 0x2322AD4[3]
), and some other data. What I am trying to achieve is separate the offset from the memory address by replacing the opening bracket with a single space and get rid of the closing bracket. The procedure I'm taking goes as follows:
- Find every digit that is followed by an opening bracket (this is because my other data may contain a bracket that should NOT be messed with).
- Replace ONLY the bracket with a single space.
- Find every digit that is followed by a closing bracket, and ONLY replace the closing bracket with a single space.
The brackets could be easily handled with the following two commands:
sed 's/\[/ /' #replace the opening bracket with a single space.
sed 's/\]//' #delete the closing bracket
But again, I don't want to mess with the other data stored in the database.
My second guess was getting back-referencing involved to handle the job. Is it possible to tell sed
to find every digit followed by an opening brace and back-reference the digit and add a space right after it, resulting in something like this:
0x2322AD4 3]
So then I can just go ahead and use the second command mentioned above to get rid of the closing brace?
If there is a whole difference approach to solve the problem, please post.