dynamically convert last binary byte

Hello!

Everytime when a specific binaryfile grows (in 4 byte steps) I want to get the newest 2 bytes immediately when they occur. That can be done by

tail -f binfile

but I need the bytes in hex format. Therefore I use hexdump

tail -f binfile | hexdump -x

what only prints out complete lines of 16 bytes. But I need the last bytes immediately when the binfile grows not waiting until 16 bytes are reached. So I tried

tail -c 2 binfile | hexdump -x

where hexdump prints the last 2 bytes only but tail stops after that and does not check the binfile constantly.

I put this into a loop, checking the binfile for changes in filesize

    while [ ]
    do
      newsize=$(ls -l $binfile | awk '{print ($5)}')
      if [ $newsize != $oldsize ] 
      then
        hexchar=$(tail -c 2 $binfile | hexdump -x)
      fi
    done

So, in principle this works but I think this is no good style and performance optimized, is it? Any ideas how to convert the byte directly out of tail -f binfile ?

Post the output of the tail - f binfile | hexdump -x command and the desired ouput (bytes).

Regards

Output example:

0000010    f708    b34c    f708    b34c    f708    ad52    f708    ad52
0000020    f708    af50    f708    b14e    f708    b34c    f708    b748
0000030    f708    af50    f708    ad52    f708    af50    f708    b34c
0000040    f708    b34c    f708    ed12    f708    f50a    f708    f10e

Output is line by line. If every 2 seconds 4 new bytes "xxxx xxxx" are appended to the binary file, then every new output line is printed after 8 seconds.

But what I need is the "xxxx xxxx" immediately. So it should be somthing like

...
f708    b34c
f708    ed12
f708    f50a
f708    f10e

every 2 seconds

Try this:

tail -f binfile | hexdump -x |
awk 'END{for(i=2;i<NF;i+=2){print $i, $(i+1)}}' 

Now there is no output at all. It's not as simple as reformating the the last line. The problem is, that hexdump collects all bytes until a comlete row of 16 byte is printed. When I use hexdump -n4 then it outputs the last 4 bytes, but tail -f stops running :frowning:

> tail -f binfile | perl -nle 'if (/(\d+) (\d+)/) {$b = $1 . $2;$i = unpack("N", pack("B32", substr("0" x 32 . $b, -32))); 
$x = sprintf("%x", $i); $x1 = substr($x,0,4); $x2 = substr($x,4,4); print "$x1 $x2"}'
f708 b34c
f708 ed12
f708 f50a
f708 f10e
^C

Thanks for your answer. Unfortunately there is no perl on that machine and I cannot test it. But perhabs I can find out how to make an executable file ...