Find the answer to your Linux question:
Results 1 to 6 of 6
I have a number of files that I need to do a sequence of operations on and was wanting to use a script (i'm new to Linux and this looks ...
  1. #1
    Just Joined!
    Join Date
    May 2008
    Posts
    2

    file edit script/code - newbie needs help

    I have a number of files that I need to do a sequence of operations on and was wanting to use a script (i'm new to Linux and this looks quite powerful!)

    One of the operations is to search within the file for the hex string 64 00 33 and replace it with the hex string 64 00 29.

    I found the command gawk but from what I can see is it only works with ASCII and not hex.

    Is there a simple C/C++ code I can use that will take the filename as an argument, open the file, search for the hex string, replace with the new string and then save the file?


    Thanks in advance.


    Russell

  2. #2
    Linux Engineer khafa's Avatar
    Join Date
    Apr 2008
    Location
    Tokyo, Japan
    Posts
    858
    HEX files are nothing but ascii files. so gawk should be able to do the work.

    but what about some sed stuff.


    suppose that the file is named some_file(if you looking for many files just change the find part)and is located in the actual directory. you could do something like
    Code:
    find . -name some_file -type f | xargs sed -i 's/64 00 33/64 00 29/g'
    Linux and me it's a love story

  3. #3
    Linux Guru
    Join Date
    Nov 2007
    Location
    Córdoba (Spain)
    Posts
    1,513
    By the wording of his post, I think that he might mean simple binary files. In that case sed or awk will not help -at least not in a direct manner-. However, od can convert any file to a plain ascii file with hex on it. Then you can grep, sed or whatever on that file.

    How to convert it back to a binary file is up to you :P

    You could also use bsdiff to create a patch from an already modified binary, and bspatch to apply that patch later.

    I am sure that googling around with this stuff can profice some results. I never felt the need to do this myself so I can't give more concrete instructions.

  4. #4
    Just Joined!
    Join Date
    May 2008
    Posts
    2
    Thanks for the reply guys.

    What I am trying to do is edit a video file. Near the start of the file there is information about the type etc which I want to change. I assume the video is stored as binary.

    If I were to do it manually, I would open the file in a hexeditor, search for "64 00 33" and replace it with "64 00 29". As I have a lot of files, I wish to do this using a script.

    I cam across the pearl code below that suggests it would do as I request but I only have a limited understanding of programming language (I use VBA) but have a strong understanding of logic. So if I get the language the logic should be easy.

    Code:
    $str_to_replace = "blah"; 
    $replace_with = "blah blah"; 
    
    # get file into array 
    open(FILE,"/usr/data/myfile"); 
    @lines = ; 
    close(FILE); 
    
    # perform string replace 
    foreach(@lines){ 
    $_ =~ s/$str_to_replace/$replace_with/g; # add i after g to make 
    case-insensitive 
    } 
    
    # write back to file 
    open(FILE,">/usr/data/myfile"); 
    foreach(@lines){ 
    print FILE $_; 
    } 
    close(FILE);
    Maybe if the above looks like it would work, someone could explain it for me and show how I could run this in Linux. Does it need compiled? Or can i just copy it to usr/local/bin and run it like that?

    Thanks again.

  5. #5
    Just Joined!
    Join Date
    Apr 2008
    Location
    Thunder Bay, Ontario
    Posts
    32
    I don't know Perl that well myself, but this should get you started.
    First off, you should make sure you have Perl - probably under /usr/bin/.
    There are two ways you can run your script:
    Code:
    perl myScript.pl
    (.pl is the file extension for Perl scripts) or you could add
    Code:
    #!/usr/bin/perl
    as the first line in your script. Assuming /usr/bin/perl is the path to Perl.

    It looks like you only need to make a few changes in the script itself.
    $str_to_replace = "64 00 33";
    $replace_with = "64 00 29";
    and change your input and output file paths.

  6. #6
    Linux Guru
    Join Date
    Nov 2007
    Location
    Córdoba (Spain)
    Posts
    1,513
    I don't remember a crap of perl myself. But I highly doubt that that file can substitute anything in binary files. I think it operates over plain text files.

    Just looking at the lines that substitures the string and the "case-insensitive" bit, I would say that at least.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...