![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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? |
|
||||
|
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!
|
|
||||
|
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? |
|
||||
|
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
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|