2

as $ hexdump -C data print the hex content of file in 16 bytes a line

How then to, by set its option, to print a line everytime the Ascii 0a (newline) is encountered ?

  • Do you mean print a blank line? Or print the line that includes 2a? [edit] your question to provide an example of what you require – Greenonline Oct 15 '21 at 04:44
  • 1
    0x0a is a newline, 0x2a is *. Do you want a newline output after 0x0a or 0x2a? – icarus Oct 15 '21 at 05:24
  • I think you mean you want one line of hex dump on output for each line of input (lines being delimited with newline characters (0xa in ASCII)), is that right? – Stéphane Chazelas Oct 15 '21 at 05:36
  • But if input lines have different lengths, the |.....| text part at the end can't be aligned. Do you still want it and misaligned? – Stéphane Chazelas Oct 15 '21 at 05:37

2 Answers2

3

If you mean you'd like one line of hex dump on output for each (newline (0xa) delimited) line of input (or 0x2a (*) delimited record), you can't really do it with hexdump, but it should be relatively easy to code it in Perl:

lhexdump() {
  perl -ne '
    printf "%08x %s |%s|\n",
      $offset,
      join(" ", unpack("(H2)*", $_)),
      s/[^[:print:]]/./rg;
      $offset = tell ARGV;
    END{printf "%08x\n", $offset}' -- "$@"
}

Note that no attempt is made to separate hex numbers in groups of 8 nor to align the text part at the end, nor to skip duplicates like hexdump does.

$ printf 'a\xff1\n**\n\b' | lhexdump
00000000 61 ff 31 0a |a.1.|
00000004 2a 2a 0a |**.|
00000007 08 |.|
00000008

For *-delimited records, add a -052 option to perl (octal 52 is hex 0x2a).

To still limit to at most 16 bytes per output line (and then be able to align the output):

lhexdump() {
  perl -ne '
    while ($_ ne "") {
      my $chunk = substr($_, 0, 16);
      $_ = substr($_, 16);
      printf "%08x %-47s |%s|\n",
        $offset,
        join(" ", unpack("(H2)*", $chunk)),
        $chunk =~ s/[^[:print:]]/./rg;
      $offset += length($chunk);
    }
    END{printf "%08x\n", $offset}' -- "$@"
}
$ head -n4 /etc/passwd | lhexdump
00000000 72 6f 6f 74 3a 78 3a 30 3a 30 3a 72 6f 6f 74 3a |root:x:0:0:root:|
00000010 2f 72 6f 6f 74 3a 2f 62 69 6e 2f 62 61 73 68 0a |/root:/bin/bash.|
00000020 64 61 65 6d 6f 6e 3a 78 3a 31 3a 31 3a 64 61 65 |daemon:x:1:1:dae|
00000030 6d 6f 6e 3a 2f 75 73 72 2f 73 62 69 6e 3a 2f 75 |mon:/usr/sbin:/u|
00000040 73 72 2f 73 62 69 6e 2f 6e 6f 6c 6f 67 69 6e 0a |sr/sbin/nologin.|
00000050 62 69 6e 3a 78 3a 32 3a 32 3a 62 69 6e 3a 2f 62 |bin:x:2:2:bin:/b|
00000060 69 6e 3a 2f 75 73 72 2f 73 62 69 6e 2f 6e 6f 6c |in:/usr/sbin/nol|
00000070 6f 67 69 6e 0a                                  |ogin.|
00000075 73 79 73 3a 78 3a 33 3a 33 3a 73 79 73 3a 2f 64 |sys:x:3:3:sys:/d|
00000085 65 76 3a 2f 75 73 72 2f 73 62 69 6e 2f 6e 6f 6c |ev:/usr/sbin/nol|
00000095 6f 67 69 6e 0a                                  |ogin.|
0000009a

Compared with:

$ head -n4 /etc/passwd | hexdump -C
00000000  72 6f 6f 74 3a 78 3a 30  3a 30 3a 72 6f 6f 74 3a  |root:x:0:0:root:|
00000010  2f 72 6f 6f 74 3a 2f 62  69 6e 2f 62 61 73 68 0a  |/root:/bin/bash.|
00000020  64 61 65 6d 6f 6e 3a 78  3a 31 3a 31 3a 64 61 65  |daemon:x:1:1:dae|
00000030  6d 6f 6e 3a 2f 75 73 72  2f 73 62 69 6e 3a 2f 75  |mon:/usr/sbin:/u|
00000040  73 72 2f 73 62 69 6e 2f  6e 6f 6c 6f 67 69 6e 0a  |sr/sbin/nologin.|
00000050  62 69 6e 3a 78 3a 32 3a  32 3a 62 69 6e 3a 2f 62  |bin:x:2:2:bin:/b|
00000060  69 6e 3a 2f 75 73 72 2f  73 62 69 6e 2f 6e 6f 6c  |in:/usr/sbin/nol|
00000070  6f 67 69 6e 0a 73 79 73  3a 78 3a 33 3a 33 3a 73  |ogin.sys:x:3:3:s|
00000080  79 73 3a 2f 64 65 76 3a  2f 75 73 72 2f 73 62 69  |ys:/dev:/usr/sbi|
00000090  6e 2f 6e 6f 6c 6f 67 69  6e 0a                    |n/nologin.|
0000009a
Toby Speight
  • 8,678
0

Looking at the man page, hexdump doesn't have an option to conditionally print a line.

Assuming that you want to print the line containing the 2a, just use grep:

hexdump -C data | grep 2a
Greenonline
  • 1,851
  • 7
  • 17
  • 23