Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

delete FF's from a file

by james28909 (Chaplain)
on Feb 11, 2013 at 09:10 UTC ( #1018108=perlquestion: print w/replies, xml ) Need Help??
james28909 has asked for the wisdom of the Perl Monks concerning the following question:

i have this file in binmode (its hex bin) and its only 190 or so kb,
the file size varies but that really doesnt matter.
what i am trying to do is open the file in read mode, then find all instances
of FF*16 or (FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF) and delete it,
but only FF*16 because there are other instances of just FF or FF FF and i would like to leave those instances and just delete FF*16. here is the code i have so far:
open($infile, '<', './file') or die "cannot open file: $!"; binmode $infile; open($outfile, '>', './modifiedfile') or die "cannot open modifiedfile +: $!"; binmode $outfile; local $/; $file = <$infile>; $file =~ tr/\FF*{16}//d; print $outfile $file; close ($infile); close ($outfile);
i had some help and figured out that $file =~ s/\0{16}//g; works great for 00's, but it doesnt work for FF's -.-

i hope you understand what i am treing to do, and thank you for any help i recieve :)
EDIT: $file =~ s{\xFF{16}}{}g; worked great.

Replies are listed 'Best First'.
Re: delete FF's from a file
by Corion (Pope) on Feb 11, 2013 at 09:18 UTC

    tr doesn't take a regular expression.

    Most likely you will want to use s/// instead.

      i did try $file =~ s/\F{16}//d as well as $file =~ s/\0{16}//g
      and it still did not work
      i did try $file =~ s/\F{16}//d as well as $file =~ s/\F{16}//g
      and it still did not work

        Neither \F nor \FF are how you represent an "FF character" in a string.

        Most likely you want \x{FF}, but you will find more information in the documentation as linked by Anonmyous Monk here already.

Re: delete FF's from a file (perlquote)
by Anonymous Monk on Feb 11, 2013 at 09:28 UTC
      :D that worked
      you guys are awesome. i would never have figured that out.
      thank you so much for your help ;)
Re: delete FF's from a file
by Lotus1 (Curate) on Feb 11, 2013 at 15:25 UTC
    i have this file in binmode (its hex bin)...

    This is a common source of confusion that people think processors use hexidecimal internally. The processor uses binary and the file is in binary. Hexidecimal is a representation shown on the screen or in a string, etc. that is easier for humans to use than binary. Searching for 'FF's in a binary file is not what you want. You want the binary value represented by the hex number 'FF'.

Re: delete FF's from a file
by igelkott (Curate) on Feb 11, 2013 at 09:32 UTC

    Several issues with this. Here are a few hints:

    • The first arg (file handle) for for open is usually bare (no $)
    • Your search is made for a plain text file, not binary data.
    • Your regular expression is way off (no offence): "\F", "F" and the 16 counts of strings of any length
    • 16 FF's would be 'FF' x 16 ... if you were going for a text string rather than binary data

    You should review common "regular expressions" and then searching binary data in particular.

      so i can open with filehandle "infile" without "$"?
      yes, it is a dump from hardware, but i guess you could call it plain text as it can be opened in notepad but has wild characters.
      i tried many different variations of $file =~ and nothing would work out.
      i am fixing to read up on the link that was posted earlier.
      thanks for all the help
        so i can open with filehandle "infile" without "$"?

        You CAN but there are good some very good reasons not to.

        You will notice from the last link above the "Modern Perl", Recommended by Anonymous Monk below, does not use it.

        At the end of the day its a choice that you make but I disagree with igelkott on this.

        To differentiate you could always call it $input_file_handle or similar.

        FWIW, you should binmode if you want to treat your binary files as binary ( no newline translation )

        with open it would be  open my($infile), '<:raw', ... or die ...

        While you're in a learning mood, get a free copy of Modern Perl book, a loose description of how experienced and effective Perl 5 programmers work....You can learn this too.</b.

        i have def bookmarked this site
        this seems to be the place i can learn alot from
        i will make me a spot in the corner
        you wont even know im here

      The first arg (file handle) for for open is usually bare (no $)

      It usually isn't, as barewords are global symbols, and lexicals are preferable in almost every situation

      You could make an argument about style (different things should look different), but you did not

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1018108]
Approved by igelkott
Front-paged by Lotus1
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2017-03-29 17:37 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (352 votes). Check out past polls.