Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I'm trying to understand the formatting option of hexdump. I have read the man page and it says that I can tell it what format to output the data in by using the -e option. However, I can not for the life of me understand the code for the option. For now, I'm just trying to have it print random lines from /dev/random like this:

6568:6C6C:7565:7568:2E62:716D:2E2C:7563

(Basically, a psuedo IPv6 address.) However, I would really like to understand the formatting strings, not just have one that does the trick. I have tried reading the man page and it wasn't much help--it seemed to refer to it being similar to C code. Well, I have almost no experience with C, so I'm stuck. Can somebody please explain this?

share|improve this question
add comment

1 Answer

up vote 5 down vote accepted

In the manual page for hexdump, there is an entire section dedicated to the structure of the format strings (search for "A format string contains any number of format units").

To get the output you are after, the following invocation should do the trick:

hexdump -e '7 2 "%04X:" 1 2 "%04X\n"' input_file

The format string here has two parts. The first is repeated 7 times, formatting two bytes at a time formatted with "%04X:" (four hexadecimal digits with zero padding, followed by a colon). The second part is repeated once only and takes two bytes formatted with "%04X\n" to complete the line of output. This process is then repeated as more data is read from the input file.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.