How to substitute a string with different size


#1

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


#2

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 removed_email_address@domain.invalid:


#3

Yang B. wrote:

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 removed_email_address@domain.invalid:

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


#4

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


#5

Tim B. wrote:

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