代码之家  ›  专栏  ›  技术社区  ›  Goyuix

二进制“尾”文件

  •  11
  • Goyuix  · 技术社区  · 16 年前

    我猜这个网站上的大多数人都熟悉tail,如果不是的话——它提供了一种“跟随”模式,当文本附加到文件后,tail会将这些字符转储到终端。

    我正在寻找的(如果有必要,我可能自己编写)是一个可以在二进制文件上工作的tail版本。基本上,我有一个无线链接,当文件从另一个网络链接下来时,我想通过它慢慢地传输文件。翻看尾部的源代码,重写并不难,但我宁愿不要重新发明轮子!这并不是严格意义上的“尾部”,因为我希望复制整个文件,但它会看到新的字节被添加并流式传输这些字节。

    思想?

    8 回复  |  直到 14 年前
        1
  •  19
  •   Adam Pierce    16 年前

    通过管道将其输送至hexdump:

    tail -f somefile | hexdump -C
    
        2
  •  6
  •   Trenton McKinney    13 年前

    还有 bintail

    这个 双翅目 包包含一个应用程序, 双翅目 . 该程序从磁盘读取一个普通文件,并将输出逐字节传输到标准输出,无需翻译,类似于 (1) 对文本文件不执行任何更改。这对于实时写入二进制文件(如WAV文件)时“跟踪”二进制文件非常有用。这个应用程序正在开发中,但它已经完成了为我设计的功能。

        3
  •  3
  •   ruief    7 年前

    LinuxCoreutilstail(1)在二进制文件上运行良好。对于大多数应用程序,您只需要避免它的行定向,这样输出就不会在数据结构中间的某个随机点开始。您只需从文件开头开始即可,这也是您要求的:

    tail -c +1 -f somefile

    很好用。

        4
  •  2
  •   Bin Tail    13 年前

    此针对Windows的快速编码Python脚本可能会有所帮助:

    # bintail.py -- reads a binary file, writes initial contents to stdout,
    # and writes new data to stdout as it is appended to the file.
    
    import time
    import sys
    import os
    import msvcrt
    msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
    
    # Time to sleep between file polling (seconds)
    sleep_int = 1
    
    def main():
        # File is the first argument given to the script (bintail.py file)
        binfile = sys.argv[1]
    
        # Get the initial size of file
        fsize = os.stat(binfile).st_size
    
        # Read entire binary file
        h_file = open(binfile, 'rb')
        h_bytes = h_file.read(128)
        while h_bytes:
            sys.stdout.write(h_bytes)
            h_bytes = h_file.read(128)
        h_file.close()
    
    
        # Loop forever, checking for new content and writing new content to stdout
        while 1:
            current_fsize = os.stat(binfile).st_size
            if current_fsize > fsize:
                h_file = open(binfile, 'rb')
                h_file.seek(fsize)
                h_bytes = h_file.read(128)
                while h_bytes:
                    sys.stdout.write(h_bytes)
                    h_bytes = h_file.read(128)
                h_file.close()
                fsize = current_fsize
            time.sleep(sleep_int)
    
    if __name__ == '__main__':
        if len(sys.argv) == 2:
            main()
        else:
            sys.stdout.write("No file specified.")
    
        5
  •  1
  •   mhawke    16 年前

    less somefile

    然后按 shift F

        6
  •  1
  •   R.. GitHub STOP HELPING ICE    14 年前

    严格地说,您需要编写一个程序来实现这一点,正如 tail 未指定用于处理二进制文件。如果您希望尽快接收新的“涓流”数据,可能还需要避免缓冲问题。

        7
  •  0
  •   wnoise    16 年前

        8
  •  0
  •   Anonymous    7 年前

    我使用它,因为它也适用于实时流:

    cat ./some_file_or_dev | hexdump -C
    

    按键(和释放)示例:

    [user@localhost input]$ sudo cat /dev/input/event2 | hexdump -C
    00000000  81 32 b1 5a 00 00 00 00  e2 13 02 00 00 00 00 00  |.2.Z............|
    00000010  04 00 04 00 36 00 00 00  81 32 b1 5a 00 00 00 00  |....6....2.Z....|
    00000020  e2 13 02 00 00 00 00 00  01 00 36 00 01 00 00 00  |..........6.....|
    00000030  81 32 b1 5a 00 00 00 00  e2 13 02 00 00 00 00 00  |.2.Z............|
    00000040  00 00 00 00 00 00 00 00  81 32 b1 5a 00 00 00 00  |.........2.Z....|
    00000050  a3 af 02 00 00 00 00 00  04 00 04 00 36 00 00 00  |............6...|
    00000060  81 32 b1 5a 00 00 00 00  a3 af 02 00 00 00 00 00  |.2.Z............|
    ^C
    
        9
  •  0
  •   Alex    3 年前

    我使用这个命令(1表示要解释的字节数): 尾部-f |外径-x1