1

I'm trying to display a hexdump of a file and highlight carriage returns in red. I'm having a really hard time understanding the format of the formats option (-e).

Is it something like:

hexdump -e "_L[red:0A]" practice_file_1.txt

?

I'm slowly solving the puzzle, but I probably don't know a higher level knowledge base that would make this more understandable, like the printf(3) syntax or something, but I don't know what it is...

Also, is it even related to the -L tag? (Do I need to use the -L option?)

References:

  • The _L has to follow a format-specifier, e.g., %c. The examples at the end of the hexdump manual page show this. – Thomas Dickey Nov 5 '16 at 0:12
  • Do you have an example of a command that would work? – Emerson Peters Nov 5 '16 at 0:15
  • Not offhand, but if I had that version of hexdump, perhaps interesting (which system are you using? I don't see the color version in Debian) – Thomas Dickey Nov 5 '16 at 0:22
  • The reason I asked is that I have tried a lot of different formats that didn't work, including adding the %c. I'm using Fedora, not sure if that is the system though. – Emerson Peters Nov 5 '16 at 0:52
2

Here is a quick example:

hexdump -f foo.cfg foo.log

and foo.cfg contains

"%06.6_ao "  12/1 "%3_c_L[red:0x0a] "
"\n"

and the result only colors when writing to a terminal (not redirected). Looking at the result by capturing the output using script, the result looks like this in a text editor:

Script started on Fri 04 Nov 2016 09:23:32 PM EDT
000000   f   o   o ^[[31m \n^[[0m   f   o   o   .   l   o   g ^[[31m \n^[[0m^M
000014   s   d   d   m   -   a   u   t   h   0   2   4^M
000030   0   b   f   b   3   -   a   d   0   6   -   4^M
000044   7   0   7   -   b   a   1   3   -   1   f   e^M
000060   0   7   5   9   e   a   e   7   4 ^[[31m \n^[[0m   s   d^M
000074   d   m   -   a   u   t   h   0   b   1   9   9^M
000110   b   f   5   -   c   7   3   4   -   4   5   f^M
000124   f   -   a   4   8   0   -   f   5   a   a   c^M
000140   e   c   3   e   1   b   0 ^[[31m \n^[[0m   s   d   d   m^M
000154   -   a   u   t   h   4   0   7   1   d   2   1^M
000170   b   -   6   0   4   7   -   4   a   8   5   -^M
000204   8   1   3   6   -   f   3   8   9   0   d   6^M
000220   a   b   a   d   0 ^[[31m \n^[[0m   s   d   d   m   -   a^M
  • Oh!!! I didn't realize the format options were in a .cfg file! I thought that they were put after the -e option in terminal. Thanks for the explanation! – Emerson Peters Nov 5 '16 at 1:30
  • in -e, you can only do simple things – Thomas Dickey Nov 5 '16 at 1:38
  • With -e (--format) you can do the same things as with the format file, just repeat the option. E. g. -e '"%06.6_ao"' -e '12/1 "%3_c_L[red:0xa]"' .... Note, you should use single quotes for the format string and double quotes for format rules inside format strings. That is: -e ' " " '. – Shamaoke Feb 10 '18 at 21:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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