LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-16-2011, 01:17 AM   #1
Zero Angel
Member
 
Registered: Jul 2010
Distribution: PinguyOS 12.04
Posts: 50

Rep: Reputation: 1
Want to remove a few bytes from the header of several binary files


I have a large set of binary wav files that I want to batch process. However the first few bytes of those files contains extraneous information that prevents the files from being opened in an audio program. So what I want to do is remove the first 12 bytes from each file, so that the file header becomes a vanilla Wave file header. I can do this in hex editors like Bless, but I don't know of one which can do it as a batch operation.

The exact hex characters I want to remove from the file are these ones:
Code:
66 73 73 6D 01 00 00 00 01 00 00 00
I figure one of you programming types would be able to help me with the problem.

Cheers.

Last edited by Zero Angel; 09-16-2011 at 01:51 AM.
 
Old 09-16-2011, 01:45 AM   #2
elonica.pl
LQ Newbie
 
Registered: Jun 2011
Posts: 21

Rep: Reputation: Disabled
Did you try to play with dd ?

http://www.unix.com/shell-programmin...ytes-file.html
http://unix.stackexchange.com/questi...tart-of-a-file
 
Old 09-16-2011, 01:59 AM   #3
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
Hello,

You could always use dd to do this:
Code:
dd if=file.wav of=file_new.wav bs=1 skip=12
Of course, this would need to be modified, and you could write a script with this to process multiple files. Let me know if this would work for you.

Cheers,

Josh

Edit - It looks like I got beat!

Last edited by corp769; 09-16-2011 at 02:02 AM.
 
Old 09-16-2011, 02:02 AM   #4
Zero Angel
Member
 
Registered: Jul 2010
Distribution: PinguyOS 12.04
Posts: 50

Original Poster
Rep: Reputation: 1
Actually, just as you posted I found a method to find/replace using perl. My test command succeeded:

Code:
perl -pe 's/\x{66}\x{73}\x{73}\x{6D}\x{01}\x{00}\x{00}\x{00}\x{01}\x{00}\x{00}\x{00}//g' < lb_sni_cap_snigen_lt_s_5201820a.smf > lb_sni_cap_snigen_lt_s_5201820a.wav
Now I need to find a way to make into a batch operation for all files in a given directory. Not sure how to do that as my scripting knowledge is fairly basic and i'm not well versed in for-each type commands.
 
Old 09-16-2011, 02:04 AM   #5
Ramurd
Member
 
Registered: Mar 2009
Location: Rotterdam, the Netherlands
Distribution: Slackwarelinux
Posts: 703

Rep: Reputation: 111Reputation: 111
No idea how this works on binary files, but you can give it a try with tail; something like this:
Code:
tail -c +12 infile > outfile
 
Old 09-16-2011, 02:08 AM   #6
Zero Angel
Member
 
Registered: Jul 2010
Distribution: PinguyOS 12.04
Posts: 50

Original Poster
Rep: Reputation: 1
I just tested corp769's solution and it seems to be the best one due to its simplicity. Now I need to find a way to do it in batch for every file in the folder. Can anyone help me with that -- or point me in the direction of a tutorial which will tell me how to batch commands like that?

Last edited by Zero Angel; 09-16-2011 at 02:09 AM.
 
Old 09-16-2011, 02:08 AM   #7
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
To save myself a bunch of typing, have a look here - http://www.cyberciti.biz/faq/bash-loop-over-file/
Looping through files in bash is a very simple task, you just need to mess with it so you get the proper output and filenames.
 
1 members found this post helpful.
Old 09-16-2011, 02:13 AM   #8
Zero Angel
Member
 
Registered: Jul 2010
Distribution: PinguyOS 12.04
Posts: 50

Original Poster
Rep: Reputation: 1
This is enough information for me to write a shell script to automate this. Thank you once again corp.
 
Old 09-16-2011, 02:15 AM   #9
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
No problem dude. I would gladly help you make the script, but I'm at work, and sadly enough, the government has a contract with microsoft, and I'm stuck using windows for the time being....
 
1 members found this post helpful.
Old 09-16-2011, 02:34 AM   #10
Zero Angel
Member
 
Registered: Jul 2010
Distribution: PinguyOS 12.04
Posts: 50

Original Poster
Rep: Reputation: 1
Code:
#!/bin/bash
for f in *.smf
do
  echo "Processing $f"
  dd if=$f of=`basename $f`.wav bs=1 skip=12
done
 
Old 09-16-2011, 02:41 AM   #11
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
Awesome dude, glad you got it!
 
Old 09-16-2011, 01:40 PM   #12
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Good work. I'd only add a few points concerning the solution.

1) Always quote your variable expansions. Otherwise the shell will apply word-splitting to the contents, and your commands will break.

http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes

Word splitting is a very important concept in scripting/shell use. Make sure you get it down.

2) Extracting substrings from variables can usually be done with parameter substitutions, making external tools like basename unnecessary most of the time.

http://mywiki.wooledge.org/BashFAQ/073

3) When you do need to use external tools, $(..) is highly recommended over `..`


Code:
#!/bin/bash

for f in *.smf ; do
  echo "Processing $f"
  dd if="$f" of="${f%.*}.wav" bs=1 skip=12
done
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
can i add my own field(of 2 bytes) in the ip header structure raklo Linux - Networking 5 08-18-2011 02:17 PM
grepping through a binary file for a sequence of bytes precision Linux - Software 5 04-07-2009 03:27 PM
How to add some bytes to a binary file? kornerr Linux - General 6 02-28-2006 11:23 AM
c-string remove first 8 bytes schneidz Programming 2 02-14-2006 11:52 AM
c header files in linux in place of header files in windows? harun_acs Programming 1 03-17-2004 02:24 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:50 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration