Edit file hexadecimal with xxd

Hi all:

I need edit a hexadecimal file.

for example:

000d530: 0000 0000 0000 0000 0000 0000 493a 004f  ............I:.O
000d540: 6374 2032 3820 3230 3135 2031 393a 3234  ct 28 2015 19:24

I need to cut the chain(black) and corresponding to hexadecimal
from the chain at the end of file

something like this serve?

xxd  file | sed '/493a/d' | xxd -r -p

thanks

Sorry to say that even with the wildest guessing I can't understand WHAT you want to do.

  • What is a "hexadecimal" file (if it's NOT a binary file)?
  • What is a chain ? Is it a character string?

Your input file has at least a human readable time stamp, so it's scarcely a real binary, nor, due to all those (binary) zeroes, a real text file. With your command pipe, you'll remove 16 (sort of) randomly positioned bytes - to what avail?

Please step back and rephrase the spec.

Yes it's a binary file.

I wish delete the chain in black (many files)

000d530: 0000 0000 0000 0000 0000 0000 493a 004f ............I:.O
000d540: 6374 2032 3820 3230 3135 2031 393a 3234 ct 28 2015 19:24

Making lots of assumptions here, but what about this? (seems too complex in sed for my taste):

xxd  file | awk '{
    # Only fields 2 thru 9 are the hex values we need to check.
    for(Idx=2;Idx<=9;++Idx){
        # Look for the problem data.
        if($Idx=="493a"){
            # Note stop location, so we can pad the rest of the line for xxd.
            Stop=Idx
            # Split off the portion before the problem data into an array, what we want will be in the first element.
            split($0,PartLine," 493a ")
            # Continue from where we left off and pad the line out with 0000
            for(Jdx=Stop;Jdx<=9;++Jdx){
                RemLine=RemLine " 0000"
            }
            # Output the portion before the problem data as well as the pad data.
            print PartLine[1]RemLine
            # Stop processing the file here.
            exit
        }
    }
    # Output any non-applicable lines
    print
}' | xxd -r -p

Are you sure you need that "-p" option on xxd?

Also, assuming the hex 493a ":I" will only occur on the even boundary, otherwise gets more fun.

In addition to what RudiC said, note that the sed portion of your script will not only remove every line that has the hexadecimal value 0x493a at an even numbered pair of bytes, it will also remove any line that contains the string "493a" in the text portion of the output and any line that contains 493a anywhere in the address field. And, it will not remove lines where 0x49 and 0x3a are adjacent byte values starting at an odd numbered byte address.

And, like RudiC... I have no idea what output you are hoping to produce.

Not sure if this is what is required...
Shell builtins around xxd as xxd has some strange quirks in spitting out a pure hexdump...
OSX 10.7.5 default bash terminal.

#�/bin/bash
# bin_edit
# Create a working binary file...
echo ""
BIN_ARRAY=( 00 00 00 00 00 00 00 00 00 00 00 00 49 3a 00 4f 63 74 20 32 38 20 32 30 31 35 20 31 39 3a 32 34 )
for n in {0..31} 
do
	echo -en "\x${BIN_ARRAY[$n]}"
done > /tmp/bin
# Prove binary exists...
xxd /tmp/bin
# Now create the edit...
HEX=( $( xxd -cols 1 -p /tmp/bin ) )
# Now edit...
NEWHEX=""
for n in $( seq 0 2 ${#HEX[@]} )
do
	OLDHEX="${HEX[$n]}${HEX[$(($n+1))]}"
	if [ "$OLDHEX" = "493a" ]
	then
		:
	else
		echo -en "$NEWHEX$OLDHEX"
	fi
done > /tmp/texthexdump
echo ""
# 0x493a now removed creating new binary file...
read -r line < /tmp/texthexdump
n=0
while [ $n -lt ${#line} ]
do
	echo -en "\x${line:$n:2}"
	n=$(($n+2))
done > /tmp/newbin
xxd /tmp/newbin
echo ""

Results:-

Last login: Fri Feb  5 22:34:02 on ttys000
AMIGA:barrywalker~> cd Desktop/Code/Shell
AMIGA:barrywalker~/Desktop/Code/Shell> ./bin_edit

0000000: 0000 0000 0000 0000 0000 0000 493a 004f  ............I:.O
0000010: 6374 2032 3820 3230 3135 2031 393a 3234  ct 28 2015 19:24

0000000: 0000 0000 0000 0000 0000 0000 004f 6374  .............Oct
0000010: 2032 3820 3230 3135 2031 393a 3234        28 2015 19:24

AMIGA:barrywalker~/Desktop/Code/Shell> _

This will take out ALL '493a' on even addresses only but odd adresses can be catered for.