The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Remove first N bytes and last N bytes from a binary file on AIX. naveendronavall Shell Programming and Scripting 1 05-24-2008 11:06 AM
How to delete initial N bytes from the starting of the file using sed? jockey007 Shell Programming and Scripting 3 11-19-2007 08:26 PM
How to delete N bytes from the starting of the file from a C function??? jockey007 High Level Programming 2 11-15-2007 10:25 PM
Rearrange bytes within a txt file yankee428 UNIX for Dummies Questions & Answers 4 06-16-2005 01:55 PM
Take a file from the system and put on tape and reset the file to 0 bytes JackieRyan26 UNIX for Dummies Questions & Answers 1 09-06-2001 05:08 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-12-2006
Loriel Loriel is offline
Registered User
  
 

Join Date: Jun 2003
Posts: 20
delete bytes from file

I'm doing a bit of hex editing with dd and I can replace values fairly simply. However, I've run across a situation where I need to delete bytes in the file and I'm not sure how to do that. For example:

Input file has:
1234567890

Output needs to be:
123abc90

I tried this:
printf 1234567890 > /tmp/test
printf abc | dd bs=1 seek=0x03 count=5 conv=notrunc of=/tmp/test

The output is:
123abc7890

Other things I've tried:
1) setting ibs and obs to different sizes
2) not using the notrunc option - that truncates everything after the info I'm inserting

The actual file I'm editing is a Mac OS X binary, so I can't actually use text manipulation - it has to be hex editing, which is why I'm using dd. I'm not stuck on using dd if something else will get the job done, but it's what I know. I only use Unix for a few scripting projects to make my life easier, so I'm probably just missing a common command that most people would know. Any suggestions?
  #2 (permalink)  
Old 04-12-2006
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
Code:
$ printf 1234567890 > test
$ dd if=test bs=1 count=3 of=test2
3+0 records in
3+0 records out
3 bytes transferred in 1 secs (3 bytes/sec)
$ printf abc >> test2
$ dd if=test bs=1 skip=8 seek=7 of=test2
2+0 records in
2+0 records out
2 bytes transferred in 1 secs (2 bytes/sec)
$ cat test2 ; echo
123abc90
$
  #3 (permalink)  
Old 04-12-2006
Loriel Loriel is offline
Registered User
  
 

Join Date: Jun 2003
Posts: 20
Thanks for the quick reply! That almost works for me. I had to change the seek to 6, however, because it leaves a null character after 123abc. It doesn't show up with cat, but a hexdump will show it. Anyways, thanks for the help!
  #4 (permalink)  
Old 04-12-2006
Loriel Loriel is offline
Registered User
  
 

Join Date: Jun 2003
Posts: 20
It's me again - I should have tested on the binary file. While this solution works, it takes a long time if the file is bigger than a few kb in size. The app I'm trying to patch is 11MB, so it's still going an hour later. I have a few of these patches to do and I don't have a week, unfortunately. Is there another faster way to destructively replace x bytes with y bytes if y<x?
  #5 (permalink)  
Old 04-12-2006
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
Show the exact commands that you used.
  #6 (permalink)  
Old 04-13-2006
Loriel Loriel is offline
Registered User
  
 

Join Date: Jun 2003
Posts: 20
The actual code has 17 offsets to process, but for my test run, I just used two. Also, keep in mind it never made it through the first cycle. I know the cycle was working (the apptest2 file kept growing, kb-by-slow-kb), but it's just too slow considering the total app size is 11 MB.

Code:
offsets=(0x5766ee 0xb020ae)
length1=(8 8)
length2=(5 5)
replace=(HOCUS POKUS)

for ((i=0;$i<${#offsets[@]};i++)); do
# Enter in the correct value
    printf ${replace[$i]} | dd bs=1 count="${length2[$i]}" seek="${offsets[$i]}" conv=notrunc of=apptest
# Create the infile
    dd if=apptest bs=1 count="$((${offsets[$i]}+${length2[$i]}))" of=apptest2
# Splice infile into original
    dd bs=1 skip="$((${offsets[$i]}+${length1[$i]}))" seek="$((${offsets[$i]}+${length2[$i]}))" of=apptest2
# Replace original so we can cycle
    mv apptest2 apptest
done
I tried using the printf approach to appending the infile, but that changes the filetype of the end result, which ruins the application. I didn't know if I could use dd to append to the end, so I simply changed the order to enter the correct value first, then cut up the file. That part of the loop works in a fraction of a second. It's the infile (and I suspect the splicing, as well) that takes so long.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:14 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0