Visit Our UNIX and Linux User Community


dynamically convert last binary byte


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting dynamically convert last binary byte
# 1  
Old 09-10-2009
PHP 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
Code:
tail -f binfile

but I need the bytes in hex format. Therefore I use hexdump
Code:
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
Code:
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

Code:
    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 ?

Last edited by maloe; 09-10-2009 at 03:39 PM.. Reason: tail [COLOR="Red"][B]- f[/B][/COLOR]
# 2  
Old 09-10-2009
Post the output of the tail - f binfile | hexdump -x command and the desired ouput (bytes).

Regards
# 3  
Old 09-10-2009
Output example:
Code:
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
Code:
...
f708    b34c
f708    ed12
f708    f50a
f708    f10e

every 2 seconds
# 4  
Old 09-10-2009
Try this:

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


Last edited by Franklin52; 09-10-2009 at 04:00 PM.. Reason: Removed space between - and f
# 5  
Old 09-10-2009
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 :-(
# 6  
Old 09-10-2009
Code:
> 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

# 7  
Old 09-12-2009
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 ...

Previous Thread | Next Thread
Test Your Knowledge in Computers #964
Difficulty: Medium
HTML4 became a W3C Recommendation in 1994.
True or False?

10 More Discussions You Might Find Interesting

1. Solaris

Convert files from binary to ASCII

I have a huge files in binary format thanks to help me in finding a way to convert these files from Binary format to ASCII format. (0 Replies)
Discussion started by: PRINCESS_RORO
0 Replies

2. Shell Programming and Scripting

Convert binary file to csv and then back to the binary format

Hello *nix specialists, Im working for a non profit organisation in Germany to transport DSL over WLAN to people in areas without no DSL. We are using Linksys WRT 54 router with DD-WRT firmware There are at the moment over 180 router running but we have to change some settings next time. So my... (7 Replies)
Discussion started by: digidax
7 Replies

3. Shell Programming and Scripting

split a string and convert to binary

Hi All, Iam new to unix scripting and I want a split a string into 4 characters each, leaving the last two characters and convert the splitted values into binary. For example: string='ffd80012ffe20000ffebfffeffea0007fff0ffd70014fff1fff0fff0fff201' this should split as ffd8 0012 ffe2 . .... (5 Replies)
Discussion started by: srinivasayedla
5 Replies

4. AIX

Convert clear text into binary

Hi I need to display the binary text of an clearn text which is sent as input to the shell script (c shell); ex: I will pass "HELLO" as input, and i should get the binary format of the text Thanks in advance Mohan (1 Reply)
Discussion started by: mKarri
1 Replies

5. Shell Programming and Scripting

convert hex to binary and send to variable

Folks, can anyone help with a script to convert hex to binary digits, and break the 32 bit binary into packs of 4 and send them to 8 different variables.Any help is sincerely appreciated. Thanks venu Its in korn shell...... (24 Replies)
Discussion started by: venu
24 Replies

6. UNIX for Dummies Questions & Answers

convert a .tr file to binary in cygwin

I would like to convert a .tr file to a binary file to use for trace purposes. Can anyone provide any insight? (0 Replies)
Discussion started by: sparklezilla3
0 Replies

7. Programming

trying to convert binary data to text using C++

i hav tried to convert binary 2D data into text using binreader and writing into text file using streamwriter. i use ReadSingle() function to convert from binary to ascii, although it works good in 1D data but not in more dimensions. the kind of values i get are -1.265369923E+038 and like ... (2 Replies)
Discussion started by: geet
2 Replies

8. UNIX for Dummies Questions & Answers

How to convert binary Unix file to text

Hi all, I have a print control file (dflt) for Oracle which is in binary. As I am going to develope an application in Window environment, I would like to reference the dflt file. But it is in binary format and I cannot access it. Anyone can suggest me how to convert the file into text or... (5 Replies)
Discussion started by: user12345
5 Replies

9. UNIX for Advanced & Expert Users

Need to convert Binary files to ascii

Dear Experts I need to read a binary file. I know for example in byte number 3801-3804 there is a 4 byte number embeded. Is there a way to extract this number from this file and then convert it to ascii via unix?? Your help would be highly appreciated. Very Best Regards Reza (5 Replies)
Discussion started by: Reza Nazarian
5 Replies

10. UNIX for Advanced & Expert Users

Convert ASCII to BINARY

Here is what I did . . . . I FTP'd several *.pdf files from a web site to a UNIX server, and did not set the transfer mode to BIN, now Adobe thinks that the documents are corrupted. Is there a way to convert the *.pdf files to Binary so that Adobe can open them again. I would just re-download... (2 Replies)
Discussion started by: pc9456
2 Replies

Featured Tech Videos