Editing Binary File


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Editing Binary File
# 1  
Old 07-17-2011
Editing Binary File

Dear Experts,

I have one binary file which contains multiple 31k (31744) records.
Each record begins with "a6 82".

So the pattern is:
Code:
  a6 82 (+31742 bytes)
  a6 82 (+31742 bytes)
  a6 82 (+31742 bytes)
  a6 82 (+31742 bytes)

There are some corrupted files where record does not begin with
a6 82, before them there are some fillers (00)
Code:
  a6 82 (+31742 bytes)
  00 00 00 a6 82 (+31742 bytes)
  00 00 a6 82 (+31742 bytes)
  00 00 00 00 00 00 a6 82 (+31742 bytes)

The number of 00 fillers is not constant.

Is there someway to edit this file via some script & change to following
pattern:
Code:
  a6 82 (+31742 bytes)
  a6 82 (+31742 bytes)
  a6 82 (+31742 bytes)
  a6 82 (+31742 bytes)


Note: the count of number of records in file is variable

Thanks,
Dhiraj

Last edited by Franklin52; 07-17-2011 at 03:24 PM.. Reason: Please use code tags for data and code samples, thank you
# 2  
Old 07-17-2011
Code:
sed 's/00 //g' file

# 3  
Old 07-17-2011
Hi Franklin52,

The file I am working with is binary file.
Not sure if sed would work on it?

Also this pattern of 00 can occur anywhere in valid data record.
I do not want it to be deleted if it is in "a6 82 (+31742 bytes)" chunk.
For example:

a6 82 00 00 00 11 22 33 00 00 00 00 (+31732 bytes)

Thanks,
Dhiraj
# 4  
Old 07-17-2011
I don't know perl and haven't used awk for binary before.. Do you have GNU gawk available? Can you show output of:

Code:
gawk 'BEGIN{FS="";RS="\xa6\x82";}{print length($0)}' binary

I got an idea...
# 5  
Old 07-18-2011
I'm no master of perl but this should do what you want:

Code:
#!/usr/bin/perl
open INFILE, "<", "binary" or die $!;
open OUTFILE, ">", "bin_fixed" or die $!;
binmode INFILE;
binmode OUTFILE;
my $data=chr(0);
while(1) {
    while(ord($data) == 0x00 && read(INFILE , $data, 1) == 1) { }
    last if ord($data) != 0xA6;
    read(INFILE , $data, 1);
    last if ord($data) != 0x82;
    last if read(INFILE , $data, 31742) != 31742;
    printf(OUTFILE  "%c%c", 0xA6, 0x82);
    print OUTFILE  $data;
    $data=chr(0);
}
close INFILE;
close OUTFILE;

This User Gave Thanks to Chubler_XL For This Post:
# 6  
Old 07-18-2011
Code:
od -An -v -tu1 binfile | tr -cs '[:digit:]' '[\n*]' | > binfile.fixed awk '
        NR >= r {
                if (int($0) == 0)
                        next
                r = NR + 31744
        }
        { printf("%c", $0) }'

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 7  
Old 07-18-2011
Thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert vi editing to text editing

Dear Guru's I'm using Putty and want to edit a file. I know we generally use vi editor to do it. As I'm not good in using vi editor, I want to convert the vi into something like text pad. Is there any option in Putty to do the same ? Thanks for your response. Srini (6 Replies)
Discussion started by: thummi9090
6 Replies

2. Shell Programming and Scripting

Convert binary file to csv and then back to the binary format

Hello *nix specialists, Im working for a non profit organisation in Germany to transport DSL over WLAN to people in areas without no DSL. We are using Linksys WRT 54 router with DD-WRT firmware There are at the moment over 180 router running but we have to change some settings next time. So my... (7 Replies)
Discussion started by: digidax
7 Replies

3. Shell Programming and Scripting

Output redirection of c binary file to a file in shell script is failing

I am struck up with a problem and that is with output redirection. I used all the ways for the redirection of the output of c binary to a file, still it is failing. Here are the different ways which I have used: ./a.out | tee -a /root/tmp.txt 2>&1 ./a.out | tee -a /root/tmp.txt 1>&1 ./a.out |... (2 Replies)
Discussion started by: Maya29988
2 Replies

4. Shell Programming and Scripting

editing line in text file adding number to value in file

I have a text file that has data like: Data "12345#22" Fred ID 12345 Age 45 Wilma Dino Data "123#22" Tarzan ID 123 Age 33 Jane I need to figure out a way of adding 1,000,000 to the specific lines (always same format) in the file, so it becomes: Data "1012345#22" Fred ID... (16 Replies)
Discussion started by: say170
16 Replies

5. Shell Programming and Scripting

Help with file editing while keeping file format intact

Hi, I am having a file which is fix length and comma seperated. And I want to replace values for one column. I am reading file line by line in variable $LINE and then replacing the string. Problem is after changing value and writing new file temp5.txt, formating of original file is getting... (8 Replies)
Discussion started by: Mruda
8 Replies

6. Programming

Editing binary files

I am working in C and need a solution for below problem: I have a binary file, which needs to be edited in such a way no data is loss. For example i have to insert 3 bytes of data at some position without changing the contents of the file. if file has data as:... (2 Replies)
Discussion started by: junaid.nehvi
2 Replies

7. Shell Programming and Scripting

Editing Binary Files in Unix

Hi, Is there a way to edit BINARY files in Unix. Or even are there any commands (shellscript/perl) through which I can replace all the occurences of a string inside a BINARY file with another string ?? (1 Reply)
Discussion started by: cool.aquarian
1 Replies

8. Solaris

compiled binary file gives "cannot execute binary file"

Hi, I have two Solaris machines. 1. SunOS X 5.8 Generic_108528-29 sun4u sparc SUNW,Sun-Blade-1500 2. SunOS Y 5.8 Generic_108528-13 sun4u sparc SUNW,Ultra-60 I am trying to buiild a project on both these machines. The Binary output file compiled on machine 2 runs on both the machines. Where... (0 Replies)
Discussion started by: scgupta
0 Replies

9. UNIX for Advanced & Expert Users

Modifying binary file by editing Hex values ?

Hi , i'm using special binary file (lotus notes) and modifying an hexadecimal address range with windows hex editor and it works fine ! The file is an AIX one and i'm forced to transfert (ftp) it before modifying it and re-transfert ! NOW i would do this directly under AIX ! I can... (4 Replies)
Discussion started by: Nicol
4 Replies

10. UNIX for Advanced & Expert Users

Editing the end of the file without loading the entire file

hi! I am a newbee. I would really appreciate if you can answer the following question: I have a huge data file, 214MB with several coloumns. I need to delete the very last line of the file. Everything I know takes a lot of time to do it ( because I have to open the file in an editor or run a... (3 Replies)
Discussion started by: Garuda
3 Replies
Login or Register to Ask a Question