CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner: Re: [acchao] Help with binary file [\r\n] removal: Edit Log


Aug 4, 2008, 7:36 PM

Views: 736
Re: [acchao] Help with binary file [\r\n] removal

uff.. I spent literally few hours (dont laugh) figuring that out,
but was a good practice to me :)
though someone more experience should check if this solution is
proper as this is my first attempt with regular expressions ;)

#!/usr/bin/perl -w 

open(INPUT, "./input.bin");
open(OUTPUT, ">./output.bin");
my $output;
$output .= $_ while(<INPUT>);
$output =~ s/(.{2})\r\n/$1/sg;
print OUTPUT $output;

my test input was:

5F 32 0D 0A    C5 23 0D 0A    38 23 0D 0A     5A 0A 0D 0A 
45 AB 0D 0A 0D 0A 0D 0A 23 12 0D 0A 7C CC 0D 0A
12 12 0D 0A 0D 0D 0D 0A 0A


5F 32    C5 23    38 23    5A 0A    45 AB    0D 0A    23 12    7C CC 
12 12 0D 0D 0A

Initial problem was in assigning lines to $_ in while loop - new iteration
after each new line character and it was not possible to use regex
inside the loop. So we had to join it to one long string.
It took me long time till I figured out that ".{2}" doesn't match \n, but
/s parameter solved that problem. Was fun ;)


(This post was edited by agent on Aug 4, 2008, 7:43 PM)

Edit Log:
Post edited by agent (Novice) on Aug 4, 2008, 7:43 PM

Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives