# Gsub and regexes on binary files

**URL:** <https://www.ruby-forum.com/t/gsub-and-regexes-on-binary-files/126347>\
**Category:** Ruby\
**Created:** [January 17, 2008, 4:41pm UTC](https://www.ruby-forum.com/t/gsub-and-regexes-on-binary-files/126347 "2008-01-17T16:41:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kyle\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f6c823/32.png) [@kyle\_s](https://www.ruby-forum.com/u/kyle_s)\
**Post date:** [January 17, 2008, 4:41pm UTC](https://www.ruby-forum.com/t/gsub-and-regexes-on-binary-files/126347/1 "2008-01-17T16:41:30Z")

</div>

I’m having a few issues with gsubbing a regex on a binary file.  
With all text, this seems to work great, but with the binary junk in a  
file, only some of the instances are replaced, not all.

What this script is supposed to do, is pull mail message out of  
quarantine, change who it’s going to, and put it back in postfix’s  
queue to send it on its way.

This is a super-simplified version of the code in question (just  
in-case you’re wondering, if you really write something like this, you  
need to change the ownership and permission of the resulting file,  
otherwise postfix won’t pick it up).

#!/usr/bin/ruby  
message\_path="/var/spool/MailScanner/quarantine/20080111/ABCDEFG"  
postfix\_outgoing\_path="/var/spool/postfix/incoming/A/ABCDEFG"  
message=File.open(message\_path,“rb”){|f| f.read()}  
message.gsub(/[a-z][a-z0-9]\*@candycompany.foo/i,“removed\_email\_address@domain.invalid”)  
outgoing\_message=File.open(postfix\_outgoing\_path,“wb”){|f|  
f.write(message)}

Err. Since the real version of this script is rather lengthy and a  
little ugly, mostly due to the command-line-option parsing, anyone  
have a suggestion on a good CLI library to use 🙂 my home-grown  
attempt is functional at best…

Thanks,  
Kyle

---

<div class="post-metadata">

**Author:** ![kyle\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f6c823/32.png) [@kyle\_s](https://www.ruby-forum.com/u/kyle_s)\
**Post date:** [January 17, 2008, 10:01pm UTC](https://www.ruby-forum.com/t/gsub-and-regexes-on-binary-files/126347/2 "2008-01-17T22:01:46Z")

</div>

On Jan 17, 2008 10:32 AM, Kyle S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’m having a few issues with gsubbing a regex on a binary file.  
> With all text, this seems to work great, but with the binary junk in a  
> file, only some of the instances are replaced, not all.

Could you send an example where the regex failed? Then we can try to  
figure out what’s different about the failure case.

-A

---

<div class="post-metadata">

**Author:** ![kyle\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f6c823/32.png) [@kyle\_s](https://www.ruby-forum.com/u/kyle_s)\
**Post date:** [January 18, 2008, 12:05am UTC](https://www.ruby-forum.com/t/gsub-and-regexes-on-binary-files/126347/3 "2008-01-18T00:05:03Z")

</div>

I’ll grab one off of the machine tomorrow. Due to some of the  
security stuff, you can only get console access to that box. Ugh.

Thanks  
–Kyle
