gpt4 book ai didi

regex - bash: oneliner 将输出中的十六进制数转换为十进制数

转载 作者:行者123 更新时间:2023-11-29 09:20:06 26 4
gpt4 key购买 nike

有没有人知道可以将某些输出通过管道传输到 bash 中的 oneliner,以匹配和转换所有出现的十六进制数并保持所有其他文本不变?

  • 可能匹配相当任意的十六进制输出应以某种方式通过正则表达式转换,即/(0x)?[0-9a-fA-F]{5,}/
  • 前导0x 不是必需的,但是也得到妥善处理
  • 如果没有,可能会出现误报指定数字的最小长度,但这不应该是问题
  • 目标是找到可以与尽可能多的包含十六进制数字的输出一起使用的解决方案

它应该像这样输出:

ssg sjas # cat /proc/net/stat/arp_cache | column -t 
entries allocs destroys hash_grows lookups hits res_failed rcv_probes_mcast rcv_probes_ucast periodic_gc_runs forced_gc_runs unresolved_discards table_fulls
0000000d 00000006 00000004 00000000 0000c3b9 0000c35e 00000000 00000000 00000000 0000965b 00000000 00000000 00000000
0000000d 0000000d 00000004 00000001 00000000 00000000 00000000 00000000 00000000 000007cd 00000000 00000000 00000000
0000000d 00000008 00000008 00000001 00000000 00000000 00000000 00000000 00000000 000006e0 00000000 00000000 00000000
0000000d 0000000a 00000008 00000000 00000000 00000000 00000002 00000000 00000000 00000704 00000000 00000000 00000000

stackoverflow 上的大多数其他问题只解决如何在数字系统之间转换单个数字的问题,只对指定列进行转换或完全取消输入中存在的格式。

期望的输出应该是这样的: )

ssg sjas # cat /proc/net/stat/arp_cache | perl -pe 's/(?:0x)?[0-9a-f]{5,}/hex($&)/ge' | column -t
entries allocs destroys hash_grows lookups hits res_failed rcv_probes_mcast rcv_probes_ucast periodic_gc_runs forced_gc_runs unresolved_discards table_fulls
13 6 4 0 50105 50014 0 0 0 38491 0 0 0
13 13 4 1 0 0 0 0 0 1997 0 0 0
13 8 8 1 0 0 0 0 0 1760 0 0 0
13 10 8 0 0 0 2 0 0 1796 0 0 0

我在过去的两个小时内无法通过 sed/awk/perl 找到一个简短可行/可靠的解决方案,所以我来了寻求帮助。

最佳答案

perl 使用 e 标志进行替换

perl -pe 's/(0x)?[0-9a-f]{5,}/hex $&/ge' file | column -t

关于regex - bash: oneliner 将输出中的十六进制数转换为十进制数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44355363/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com