使用原始内容恢复 hexdump 星号

使用原始内容恢复 hexdump 星号

假设我生成了两个十六进制转储,其中包括星号。第一个文件(xxd -r作品):

hexdump random.dat
0000000 6161 6161 6161 6161 6161 6161 6161 6161
*
0000020 6161 6261 6262 6262 6262 6262 6262 6262
0000030 6262 6262 6262 6262 6262 6262 6262 6262
*
00000b0

第二个文件(xxd -r不起作用):

hexdump data2.dat
0000000 6161 6161 6161 6161 6161 6161 6161 6161
*
0000020 6161 6261 6262 6262 6262 6262 6262 6262
0000030 6262 6262 6262 6262 6262 6262 6262 6262
*
00000b0 000a
00000b1

希望能够在没有压缩的情况下生成原始转储,并且没有原始文件。正是这样:

hexdump -v random.dat
0000000 6161 6161 6161 6161 6161 6161 6161 6161
0000010 6161 6161 6161 6161 6161 6161 6161 6161
0000020 6161 6261 6262 6262 6262 6262 6262 6262
0000030 6262 6262 6262 6262 6262 6262 6262 6262
0000040 6262 6262 6262 6262 6262 6262 6262 6262
0000050 6262 6262 6262 6262 6262 6262 6262 6262
0000060 6262 6262 6262 6262 6262 6262 6262 6262
0000070 6262 6262 6262 6262 6262 6262 6262 6262
0000080 6262 6262 6262 6262 6262 6262 6262 6262
0000090 6262 6262 6262 6262 6262 6262 6262 6262
00000a0 6262 6262 6262 6262 6262 6262 6262 6262
00000b0
hexdump -v data2.dat
0000000 6161 6161 6161 6161 6161 6161 6161 6161
0000010 6161 6161 6161 6161 6161 6161 6161 6161
0000020 6161 6261 6262 6262 6262 6262 6262 6262
0000030 6262 6262 6262 6262 6262 6262 6262 6262
0000040 6262 6262 6262 6262 6262 6262 6262 6262
0000050 6262 6262 6262 6262 6262 6262 6262 6262
0000060 6262 6262 6262 6262 6262 6262 6262 6262
0000070 6262 6262 6262 6262 6262 6262 6262 6262
0000080 6262 6262 6262 6262 6262 6262 6262 6262
0000090 6262 6262 6262 6262 6262 6262 6262 6262
00000a0 6262 6262 6262 6262 6262 6262 6262 6262
00000b0 000a
00000b1

所以程序是:

  1. 从文件(或从标准输入)读取转储。
  2. 对于每个星号:
    • 读取下一行开始处的结束偏移量。
    • 读取上一行开头处的起始偏移量。
    • 让它们休息以确定需要插入多少行。
    • 插入那么多行,偏移量相应增加。
  3. 将整个转储输出到另一个文件或标准输出。

答案1

尝试

awk '
/^\*/   {GAP = 1                                # check if action needed
         next                                   # don''t print, proceed to next line
        }
GAP     {TGT = sprintf ("%d", "0x" $1) + 0      # if action, calculate the end target
         do     {printf "%07x %s\n", L1, L0     # loop printing identical lines
                 L1 += 16                       # increment the first field
                }
         while (TGT > L1)                       # until target reached
         GAP = 0                                # reset action flag
        }
        {L1 = sprintf ("%d", "0x" $1) + 16      # save "to come" first field
         L0 = $0                                # and rest of line
         sub ("^" $1 FS, _, L0)
        }
1                                               # print input line
' file2
0000000 6161 6161 6161 6161 6161 6161 6161 6161
0000010 6161 6161 6161 6161 6161 6161 6161 6161
0000020 6161 6261 6262 6262 6262 6262 6262 6262
0000030 6262 6262 6262 6262 6262 6262 6262 6262
0000040 6262 6262 6262 6262 6262 6262 6262 6262
0000050 6262 6262 6262 6262 6262 6262 6262 6262
0000060 6262 6262 6262 6262 6262 6262 6262 6262
0000070 6262 6262 6262 6262 6262 6262 6262 6262
0000080 6262 6262 6262 6262 6262 6262 6262 6262
0000090 6262 6262 6262 6262 6262 6262 6262 6262
00000a0 6262 6262 6262 6262 6262 6262 6262 6262
00000b0 000a
00000b1

答案2

@RudiC 非常感谢。您的脚本适用于awk( original-awk) 和mawk,但不适用于gawk.事先检查一下是哪个开发者以及版本是什么。您也可以使用namei /usr/bin/awk.某些 linux 发行版 / *BSD 可能包含它的任何版本,并且是任何其他发行版的符号链接。

很多时候,“awk”只是 gawk、original-awk 或 mawk 的符号链接。

hexdump.exe random.dat | gawk '
/^\*/   {GAP = 1                                # check if action needed
         next                                   # don''t print, proceed to next line
        }
GAP     {TGT = sprintf ("%d", "0x" $1) + 0      # if action, calculate the end target
         do     {printf "%07x %s\n", L1, L0     # loop printing identical lines
                 L1 += 16                       # increment the first field
                }
         while (TGT > L1)                       # until target reached
         GAP = 0                                # reset action flag
        }
        {L1 = sprintf ("%d", "0x" $1) + 16      # save "to come" first field
         L0 = $0                                # and rest of line
         sub ("^" $1 FS, _, L0)
        }
1                                               # print input line
'
0000000 6161 6161 6161 6161 6161 6161 6161 6161
0000010 6161 6161 6161 6161 6161 6161 6161 6161
0000020 6161 6261 6262 6262 6262 6262 6262 6262
0000030 6262 6262 6262 6262 6262 6262 6262 6262
0000010 6262 6262 6262 6262 6262 6262 6262 6262
00000b0
hexdump.exe data2.dat | gawk '
> /^\*/   {GAP = 1                                # check if action needed
>          next                                   # don''t print, proceed to next line
>         }
> GAP     {TGT = sprintf ("%d", "0x" $1) + 0      # if action, calculate the end target
>          do     {printf "%07x %s\n", L1, L0     # loop printing identical lines
>                  L1 += 16                       # increment the first field
>                 }
>          while (TGT > L1)                       # until target reached
>          GAP = 0                                # reset action flag
>         }
>         {L1 = sprintf ("%d", "0x" $1) + 16      # save "to come" first field
>          L0 = $0                                # and rest of line
>          sub ("^" $1 FS, _, L0)
>         }
> 1                                               # print input line
> '
0000000 6161 6161 6161 6161 6161 6161 6161 6161
0000010 6161 6161 6161 6161 6161 6161 6161 6161
0000020 6161 6261 6262 6262 6262 6262 6262 6262
0000030 6262 6262 6262 6262 6262 6262 6262 6262
0000010 6262 6262 6262 6262 6262 6262 6262 6262
00000b0 000a
00000b1

相关内容