AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / unix / 问题 / 528676
Accepted
LFMekz
LFMekz
Asked: 2019-07-07 04:21:14 +0800 CST2019-07-07 04:21:14 +0800 CST 2019-07-07 04:21:14 +0800 CST

让 xxd 显示最上面一列的字节偏移量?

  • 772

所以我使用具有惊人的十六进制模式的emacs来查看文件中的字节偏移量,类似于十六进制值:

87654321  0011 2233 4455 6677 8899 aabb ccdd eeff  0123456789abcdeff             
00000000: 5765 6c63 6f6d 6520 746f 2047 4e55 2045  Welcome to GNU E

作为这种能力的粉丝。想知道这是否是我可以在 xxd 或 hexdump 中退出的功能?或者,如果有人有一个 awk 脚本来执行此操作并使其正确排列

columns hex
  • 1 1 个回答
  • 1944 Views

1 个回答

  • Voted
  1. Best Answer
    Stephen Harris
    2019-07-07T07:45:19+08:002019-07-07T07:45:19+08:00

    我最喜欢使用hexdump这种格式:

    hexdump -v -e '"%08_ax  "' -e '16/1 "%02X ""  "" "' -e '16/1 "%_p""\n"'
    

    这给出了类似于

    % echo hello there everyone | hexdump -v -e '"%08_ax  "' -e '16/1 "%02X ""  "" "' -e '16/1 "%_p""\n"'
    00000000  68 65 6C 6C 6F 20 74 68 65 72 65 20 65 76 65 72   hello there ever
    00000010  79 6F 6E 65 0A                                    yone.
    

    很容易echo在前面放一个:

    echo hello there everyone | (echo '87654321  00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff   0123456789abcdef' ; hexdump -v -e '"%08_ax  "' -e '16/1 "%02X ""  "" "' -e '16/1 "%_p""\n"')
    87654321  00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff   0123456789abcdef
    00000000  68 65 6C 6C 6F 20 74 68 65 72 65 20 65 76 65 72   hello there ever
    00000010  79 6F 6E 65 0A 
    

    或者,我们可以“分页”输出;例如,每 16 行放置一个标题,使用一个简单的awk过滤器:

    cat x | hexdump -v -e '"%08_ax  "' -e '16/1 "%02X ""  "" "' -e '16/1 "%_p""\n"' | awk '(NR-1)%16 == 0 { print "\n87654321  00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff   0123456789abcdef"} ; { print }' | less
    
    87654321  00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff   0123456789abcdef
    00000000  54 68 69 73 20 69 73 20 6C 69 6E 65 20 31 0A 54   This is line 1.T
    00000010  68 69 73 20 69 73 20 6C 69 6E 65 20 32 0A 54 68   his is line 2.Th
    00000020  69 73 20 69 73 20 6C 69 6E 65 20 33 0A 54 68 69   is is line 3.Thi
    00000030  73 20 69 73 20 6C 69 6E 65 20 34 0A 54 68 69 73   s is line 4.This
    00000040  20 69 73 20 6C 69 6E 65 20 35 0A 54 68 69 73 20    is line 5.This 
    00000050  69 73 20 6C 69 6E 65 20 36 0A 54 68 69 73 20 69   is line 6.This i
    00000060  73 20 6C 69 6E 65 20 37 0A 54 68 69 73 20 69 73   s line 7.This is
    00000070  20 6C 69 6E 65 20 38 0A 54 68 69 73 20 69 73 20    line 8.This is 
    00000080  6C 69 6E 65 20 39 0A 54 68 69 73 20 69 73 20 6C   line 9.This is l
    00000090  69 6E 65 20 31 30 0A 54 68 69 73 20 69 73 20 6C   ine 10.This is l
    000000a0  69 6E 65 20 31 31 0A 54 68 69 73 20 69 73 20 6C   ine 11.This is l
    000000b0  69 6E 65 20 31 32 0A 54 68 69 73 20 69 73 20 6C   ine 12.This is l
    000000c0  69 6E 65 20 31 33 0A 54 68 69 73 20 69 73 20 6C   ine 13.This is l
    000000d0  69 6E 65 20 31 34 0A 54 68 69 73 20 69 73 20 6C   ine 14.This is l
    000000e0  69 6E 65 20 31 35 0A 54 68 69 73 20 69 73 20 6C   ine 15.This is l
    000000f0  69 6E 65 20 31 36 0A 54 68 69 73 20 69 73 20 6C   ine 16.This is l
    
    87654321  00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff   0123456789abcdef
    00000100  69 6E 65 20 31 37 0A 54 68 69 73 20 69 73 20 6C   ine 17.This is l
    00000110  69 6E 65 20 31 38 0A 54 68 69 73 20 69 73 20 6C   ine 18.This is l
    

    我可能想在其中放置一些分隔符,以便更容易区分“标题”和内容。

    这很容易变成一个脚本:

    % cat hex 
    #!/bin/sh
    
    hexdump -v -e '"%08_ax  "' -e '16/1 "%02X ""  "" "' -e '16/1 "%_p""\n"' | awk '(NR-1)%16 == 0 { print "\n87654321  00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff   0123456789abcdef\n========  == == == == == == == == == == == == == == == ==   ================"} ; { print }'
    

    现在你可以做

    % hex < x
    

    或者

    % cat x | hex
    

    和类似的命令。

    87654321  00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff   0123456789abcdef
    ========  == == == == == == == == == == == == == == == ==   ================
    00000000  54 68 69 73 20 69 73 20 6C 69 6E 65 20 31 0A 54   This is line 1.T
    00000010  68 69 73 20 69 73 20 6C 69 6E 65 20 32 0A 54 68   his is line 2.Th
    00000020  69 73 20 69 73 20 6C 69 6E 65 20 33 0A 54 68 69   is is line 3.Thi
    00000030  73 20 69 73 20 6C 69 6E 65 20 34 0A 54 68 69 73   s is line 4.This
    00000040  20 69 73 20 6C 69 6E 65 20 35 0A 54 68 69 73 20    is line 5.This 
    00000050  69 73 20 6C 69 6E 65 20 36 0A 54 68 69 73 20 69   is line 6.This i
    00000060  73 20 6C 69 6E 65 20 37 0A 54 68 69 73 20 69 73   s line 7.This is
    00000070  20 6C 69 6E 65 20 38 0A 54 68 69 73 20 69 73 20    line 8.This is 
    00000080  6C 69 6E 65 20 39 0A 54 68 69 73 20 69 73 20 6C   line 9.This is l
    00000090  69 6E 65 20 31 30 0A 54 68 69 73 20 69 73 20 6C   ine 10.This is l
    000000a0  69 6E 65 20 31 31 0A 54 68 69 73 20 69 73 20 6C   ine 11.This is l
    000000b0  69 6E 65 20 31 32 0A 54 68 69 73 20 69 73 20 6C   ine 12.This is l
    000000c0  69 6E 65 20 31 33 0A 54 68 69 73 20 69 73 20 6C   ine 13.This is l
    000000d0  69 6E 65 20 31 34 0A 54 68 69 73 20 69 73 20 6C   ine 14.This is l
    000000e0  69 6E 65 20 31 35 0A 54 68 69 73 20 69 73 20 6C   ine 15.This is l
    000000f0  69 6E 65 20 31 36 0A 54 68 69 73 20 69 73 20 6C   ine 16.This is l
    
    87654321  00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff   0123456789abcdef
    ========  == == == == == == == == == == == == == == == ==   ================
    00000100  69 6E 65 20 31 37 0A 54 68 69 73 20 69 73 20 6C   ine 17.This is l
    
    • 3

相关问题

  • 在 awk 中调用数组以创建具有固定宽度列的表

  • 如何很好地显示制表符分隔文件中的列?

  • 如何在第一列的内容之后添加文件名?

  • 如何在其他两列输出之间插入一列?

  • 加入什么都不返回

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    模块 i915 可能缺少固件 /lib/firmware/i915/*

    • 3 个回答
  • Marko Smith

    无法获取 jessie backports 存储库

    • 4 个回答
  • Marko Smith

    如何将 GPG 私钥和公钥导出到文件

    • 4 个回答
  • Marko Smith

    我们如何运行存储在变量中的命令?

    • 5 个回答
  • Marko Smith

    如何配置 systemd-resolved 和 systemd-networkd 以使用本地 DNS 服务器来解析本地域和远程 DNS 服务器来解析远程域?

    • 3 个回答
  • Marko Smith

    dist-upgrade 后 Kali Linux 中的 apt-get update 错误 [重复]

    • 2 个回答
  • Marko Smith

    如何从 systemctl 服务日志中查看最新的 x 行

    • 5 个回答
  • Marko Smith

    Nano - 跳转到文件末尾

    • 8 个回答
  • Marko Smith

    grub 错误:你需要先加载内核

    • 4 个回答
  • Marko Smith

    如何下载软件包而不是使用 apt-get 命令安装它?

    • 7 个回答
  • Martin Hope
    user12345 无法获取 jessie backports 存储库 2019-03-27 04:39:28 +0800 CST
  • Martin Hope
    Carl 为什么大多数 systemd 示例都包含 WantedBy=multi-user.target? 2019-03-15 11:49:25 +0800 CST
  • Martin Hope
    rocky 如何将 GPG 私钥和公钥导出到文件 2018-11-16 05:36:15 +0800 CST
  • Martin Hope
    Evan Carroll systemctl 状态显示:“状态:降级” 2018-06-03 18:48:17 +0800 CST
  • Martin Hope
    Tim 我们如何运行存储在变量中的命令? 2018-05-21 04:46:29 +0800 CST
  • Martin Hope
    Ankur S 为什么 /dev/null 是一个文件?为什么它的功能不作为一个简单的程序来实现? 2018-04-17 07:28:04 +0800 CST
  • Martin Hope
    user3191334 如何从 systemctl 服务日志中查看最新的 x 行 2018-02-07 00:14:16 +0800 CST
  • Martin Hope
    Marko Pacak Nano - 跳转到文件末尾 2018-02-01 01:53:03 +0800 CST
  • Martin Hope
    Kidburla 为什么真假这么大? 2018-01-26 12:14:47 +0800 CST
  • Martin Hope
    Christos Baziotis 在一个巨大的(70GB)、一行、文本文件中替换字符串 2017-12-30 06:58:33 +0800 CST

热门标签

linux bash debian shell-script text-processing ubuntu centos shell awk ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve