Edit file hexadecimal with xxd


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Edit file hexadecimal with xxd
# 1  
Old 02-05-2016
Edit file hexadecimal with xxd

Hi all:

I need edit a hexadecimal file.

for example:

Code:
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?

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

thanks

Last edited by Don Cragun; 02-05-2016 at 12:03 PM.. Reason: Add CODE tags.
# 2  
Old 02-05-2016
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.
# 3  
Old 02-05-2016
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
# 4  
Old 02-05-2016
Making lots of assumptions here, but what about this? (seems too complex in sed for my taste):
Code:
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.
# 5  
Old 02-05-2016
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.
# 6  
Old 02-05-2016
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.
Code:
#¡/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:-
Code:
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Generate hexadecimal

Hello I'm working on a script to list all ipv6 from given address so I've run this script which create hex part of ipv6 STR2=159 END2=200 SUM2=`expr $END2 - $STR2` for ((i=STR2;i<=END2;++i)); do x=$( printf "%x" $i ) ; echo $x echo -e "::"$x >> netpart.txt done output is : ::9f... (2 Replies)
Discussion started by: nimafire
2 Replies

2. Shell Programming and Scripting

To convert file with decimal to another file with Hexadecimal

I have a text file of alphanumeric values listed one by one. I have to convert them to hexadecimal equivalents for each character seperated by ":" in Unix bash shell script. For example, 12345678 has to be converted to 31:32:33:34:35:36:37:38 (10 Replies)
Discussion started by: mathie
10 Replies

3. HP-UX

Editing a UNIX file in Hexadecimal format

Hi, I am a newbie to the UNIX world. I am asked to edit the file in hexadecimal format and save it. Later I should be able to print the file in char mode. please anyone tell me how to do that mostly using VI editor. (1 Reply)
Discussion started by: vkudire
1 Replies

4. Shell Programming and Scripting

Reverse hexdump without xxd

The "hexdump" command cannot perform reverse operation. On the other hand, the "xxd" command with -r option performs reverse hexdump, while the "xxd" command without -r performs the (forward) hexdump. An example of hexdump is to convert ABCD into 41 42 43 44. An example of reverse hexdump is to... (3 Replies)
Discussion started by: LessNux
3 Replies

5. Programming

Hexadecimal to ascii

Let's suppose i have a hexadecimal array with 16 cells.for example b3e2d5f636111780 i want to convert it to an array of ascii characters(in C) so that i can reduce total size of the file i want to put it in. But i am afraid i have not fully understand the difference between ascii and hex(i... (3 Replies)
Discussion started by: bashuser2
3 Replies

6. Shell Programming and Scripting

Script to Edit the file content and create new file

I have a requirement, which is as follows *. Folder contains list of xmls. Script has to create new xml files by copying the existing one and renaming it by appending "_pre.xml" at the end. *. Each file has multiple <Name>fileName</Name> entry. The script has to find the first occurance of... (1 Reply)
Discussion started by: sudesh.ach
1 Replies

7. UNIX for Dummies Questions & Answers

Hexadecimal to Decimal

Hi all, I have a small script to convert my HexaDecimal Input to Decimal as output. #!/bin/ksh hd=00208060 dec=`printf %d $hd` echo $dec Output of the above program: printf: 00208060 not completely converted 16 But my expected output is "2130016". How can i acheive this. I... (2 Replies)
Discussion started by: Arunprasad
2 Replies

8. Shell Programming and Scripting

Edit value in File

I have a file oratab with entry like this SCADAG:/esitst1/oracle/product/9.2.0.8:Y I am trying to discover a way to change the 9.2.0.8 part of this to something like 10.2.0.4 as part of an upgrade script. I have tried cat /etc/oratab >>/tmp/oratab... (1 Reply)
Discussion started by: sewood
1 Replies

9. UNIX for Dummies Questions & Answers

edit a .fs file

I have a .fs file that I want to edit, (or just be able to see what is in it) preferably through a windows environment. Does anyone know how to do that? Thanks (4 Replies)
Discussion started by: kiterboy
4 Replies

10. Shell Programming and Scripting

Get Hexadecimal Value

I have a record in a file that ends with the new line character "\n". How dio I determine the hexadecimal value for that? (2 Replies)
Discussion started by: lesstjm
2 Replies
Login or Register to Ask a Question