How to substitute a string with different size

L

Li Chen

Hi all,

I deal with a mixture of text and binary file. I want to search a
pattern in the string and replace it with a new pattern, then write to a
new file. I use the following script(only part of the script)

str.gsub!(/old_pattern/,"new_pattern")
file.seek(-str.length,IO::SEEK_CUR)
file.print str

It works fine if the both old_pattern and new_pattern are the same size.
But it will produce unpreditable result if the sizes are different. I
wonder if any one out there knows how to explain it and provide a Ruby
way solution.

Thanks,

Li
 
L

Li Chen

Yang said:
Hi Li,

What do you mean 'unpreditable result'? Do you mean that the size of
'str'
object is not correct?


2007/7/31, Li Chen <[email protected]>:

Actually what I try to do is to replace one string with another string
in some FACS data files, in which part of them are headers in text
format and the rest of them in binary format. For example replace "CD3"
with "CD4" or "CD45". If it is the first case the new file is of same
size and I can open the new file without losing the image information
but I will lost all the image information if I replace "CD3" with
"CD45". This is what I mean by unpreditable result.

Li
 
T

Tim Becker

Actually what I try to do is to replace one string with another string
in some FACS data files, in which part of them are headers in text
format and the rest of them in binary format. For example replace "CD3"
with "CD4" or "CD45". If it is the first case the new file is of same
size and I can open the new file without losing the image information
but I will lost all the image information if I replace "CD3" with
"CD45". This is what I mean by unpreditable result.

It sounds like there's some sort of index at the beginning of the file
you're changing. E.g. at the beginning of the file it says: 3 bytes of
data, followed by an image. If you substitute "CD3" for "CD45", you'd
also have to update the index at the beginning to say "4 bytes of
data, followed by an image".

I'm afraid I don't know FACS, but it sure sounds like the nature of
the file format doesn't allow you to just search and replace in the
file without updating some context information as well.
-tim
 
L

Li Chen

Tim said:
It sounds like there's some sort of index at the beginning of the file
you're changing. E.g. at the beginning of the file it says: 3 bytes of
data, followed by an image. If you substitute "CD3" for "CD45", you'd
also have to update the index at the beginning to say "4 bytes of
data, followed by an image".

I'm afraid I don't know FACS, but it sure sounds like the nature of
the file format doesn't allow you to just search and replace in the
file without updating some context information as well.
-tim

I don't know FACS either but I ask the technical services and they say
it is a kind of meta data file. If I insert extra characters into the
file I have to remove equal extra spaces from somewhere else in the
file.

Li
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,998
Latest member
MarissaEub

Latest Threads

Top