Friday, February 15, 2008

perl one line hex viewer

Following can be helpful in the case, when you have to have a look on hex dump on some Unix host, you are not satisfied with od -x output, and you have no hex viewing or hex editing utilities like hexedit, hexdump, mc mode for editing hex, and even %!xxd in vi fails because all this tools are not installed on the host were your logs, traces, core dumps, images e.t.c. placed and have to be analyzed.

But you are lucky, you have perl 5.8.x installed on the host, so you can use the script written by me for the purpose described.And it can be easily modified for formatting hex output in way you like (for example, print original hex string from stdin with it hex presentaion, or reformat in any way you like)

Session below demonstrates the use of one-line perl script for displaying hex :

Blue is input
Red is comments
Green - output used for verifying
Not marked by color - output



----------------%<-------------
rtg@kubic-roman:~/develop/perl$ cat robo.log
stay
stay
stay
stay
stay
get command: help
look up
move north
problem: fuel 11%
move east
lookup

rtg@kubic-roman:~/develop/perl$ cat robo.log | perl -ne
'map {print sprintf "%x ", ord($_)} split(//)' ; echo

73 74 61 79 a 73 74 61 79 a 73 74 61 79 a 73 74 61 79 a 73 74
61 79 a 67 65 74 20 63 6f 6d 6d 61 6e 64 3a 20 68 65 6c 70 a
6c 6f 6f 6b 20 75 70 a 6d 6f 76 65 20 6e 6f 72 74 68 a 70 72
6f 62 6c 65 6d 3a 20 66 75 65 6c 20 31 31 25 a 6d 6f 76 65 20
65 61 73 74 a 6c 6f 6f 6b 75 70 a a
# some verification
rtg@kubic-roman:~/develop/perl$ wc robo.log
12 18 98 robo.log
rtg@kubic-roman:~/develop/perl$ cat robo.log |
perl -ne 'map {print sprintf "%x ", ord($_)} split(//)' | wc

0 98 282
----------------->%------------

15 feb,2008, 0:54 initial version of article written.

No comments: