Hexdump of a string starting at new lines?
6,868
Here is one possibility, a compact solution which is making use of read
's capability to restrict the amount of read characters:
c=0
while IFS= read -n16 -r line
do
len=${#line}
((len<16)) && { ((len++)) ; line+=$'\n' ;}
printf "%08x " $c
for ((i=0; i<len; i++))
do printf " %02x" "'${line:i:1}"
done
printf " %*s %s\n" $((50-3*len)) "" "'${line//[^[:print:]]/.}'"
((c+=len))
done
Related videos on Youtube
Author by
sdaau
Updated on September 18, 2022Comments
-
sdaau over 1 year
Say I have a multi-line strings, but the entries on it are short; if I try to hexdump, then I get something like this:
echo "something is being written here" | hexdump -C #00000000 73 6f 6d 65 74 68 69 6e 67 0a 69 73 0a 62 65 69 |something.is.bei| #00000010 6e 67 0a 77 72 69 74 74 65 6e 0a 68 65 72 65 0a |ng.written.here.| #00000020
Most hex dump programs, including
hexdump
simply function as a 2D matrix (you can define how many bytes/column you're going to have per line); and so in this case, the entire output is compacted on two lines of dump.Is there a program that I can use, which would keep going as usual - except when it encounters a new line (
0x0a
- but possibly any other character, or seqence thereof), it would also start a new line? In this case, I'd imagine an output like:00000000 73 6f 6d 65 74 68 69 6e 67 0a |something.| 0000000a 69 73 0a |is.| 0000000d 62 65 69 6e 67 0a |being.| 00000013 77 72 69 74 74 65 6e 0a |written.| 0000001b 68 65 72 65 0a |here.| 00000020
-
Janis about 9 yearsDoes no answer fit your needs? - What's missing for you?
-
-
trs about 6 yearsI would have loved to upvote your answer because it's awesome. But 3 years on it doesn't quite work. Sample file content per hexdump:
01 0c 02 98 00 01 97 be 0a 16 00 00
Output of your function:00 01 0c 02 7ffe 01 7ffe 7ffe 0a