How to replace 10th and 11th byte in a hex dump
Hello everybody!
I have a feeling that there is a very simple and efficient solution to my task I simply forgot. If I am wrong and you are a mod would you move this to Programming please? I am working with hex dump looking like that Code:
01 74 58 d9 00 1e 4c 08 00 21 85 69 57 40 00 50 ea 74 58 d9 00 5e 6d cb 61 74 31 70 ... My intended usage is Code:
prog1 | missing_command | prog2 Regards, Heraton |
You could consider the OR operation....
Quote:
Just a flake of a thought... Thor |
Hope this helps
Code:
~[lizard]$ echo $example |
Here's a simple gawk one-liner to do it:
Code:
$ echo "01 74 58 d9 00 1e 4c 08 00 21 85 69 57 40 00 50 ea 74 58 d9 00 5e 6d cb 61 74 31 70" | gawk -v n=5 -v new=FF '{print $0;$n=new;print $0}' | cat
|
Thanks a lot!
Thank you very much for your replies!
In fact both the sed as well as the awk example do perform very well, although I have to admit that the sed thing is out of my league. The awk thing rings a bell and looks pretty familiar, so I guess that was, what I had seen before. Although I already have figured out some ways to automate replacing 4 or 6 byte in a line of hex dump, I assume I am doing things that would let more advanced scripting people run away in sheer terror. I could pipe things 4 to 6 times, which solves the problem nicely but is anything but high performance... Code:
cat Data | awk_repl <n> <Val1> | awk_repl <n+1> <Val2> | \ Code:
#!/bin/bash Any suggestions would be highly appreciated. Regards, Heraton |
Well, you could do it in a few different ways. If you only want to cover the case of a set of contiguous lines, this might work:
Code:
gawk -v start=5 -v new=A1:B2:C3 'BEGIN{n=split(new,New,":")}{for(i=1;i<=n;++i)$(i+start-1)=New[i];print $0} Code:
$ echo "01 74 58 d9 00 1e 4c 08 00 21 85 69 57 40 00 50 ea 74 58 d9 00 5e 6d cb 61 74 31 70" |gawk -v start=5 -v new=A1:B2:C3 'BEGIN{n=split(new,New,":")}{for(i=1;i<=n;++i)$(i+start-1)=New[i];print $0}' A more general solution would be to prepare a table of byte numbers and new values, read that in, and then apply it to each line of the input file. (You could even use a three column input table with {byte number, old byte, new byte}, and only replace the bytes when all the old bytes match.) That would be possible as a one-line program, but it would be a long line. Here's such a program written so you could put it in a file, make it executable, and use it as you middle command. (Note that the name of the pattern table is hard codded. It could be passed as a variable, but I was getting lazy.) N.B.: This is untested code. Code:
#!/usr/bin/gawk -f |
Thanks a lot!
Thank you very much for your answer. Once more your solution is perfectly suitable for my task. For now I'll stick with your first proposal as I need to meet a deadline which expires on Thursday and I am already taking night shifts. But I promise to take a closer look at your second proposal next weekend, as I am sure I can learn quite a lot of that.
Regards, Heraton |
Just as an alternative, you might like to know that substr() in Perl can also do assignments as well as extractions; neat huh ;)
http://perldoc.perl.org/functions/substr.html See also pack & unpack http://perldoc.perl.org/functions/pack.html http://perldoc.perl.org/functions/unpack.html |
Good to know
Thanks for that advice. I guess I will take a look at that soon. Right now I have to meet a deadline and Perl would not be compatible with my intended audience. Anyway, I guess enlightment is waiting down that road too.
Regards, Heraton |
All times are GMT -5. The time now is 01:52 PM. |