Perl/awk/bash:乘法和求和值将值内联而不是原始文本

Perl/awk/bash:乘法和求和值将值内联而不是原始文本

我有这样的文字:

;---------------------------------------
JOURNEY_ARRAY_01
    .BYTE <JOURNEY_00
    .BYTE <JOURNEY_01
;---------------------------------------
JOURNEY_ARRAY_03
    .BYTE JOURNEY_01-JOURNEY_00
    .BYTE JOURNEY_02-JOURNEY_01
;---------------------------------------
JOURNEY_00                              ; From 00 to 01
    .BYTE $01, $01, $01, $03, $01, $01, $03, $01
    .BYTE $03, $01, $03, $01, $03, $01, $03, $01
    .BYTE $03, $03, $01, $03, $03, $03, $03, $00
    .BYTE $03, $03, $00, $03, $00, $03, $00, $03
    .BYTE $00, $03, $00, $03, $03, $00, $03, $03
    .BYTE $00, $03, $03, $03, $03, $03, $03, $01
    .BYTE $03, $03, $01, $03, $03, $01, $03
;---------------------------------------
JOURNEY_01                              ; From 00 to 04
    .BYTE $01, $01, $01, $03, $01, $01, $03, $01
    .BYTE $03, $01, $03, $01, $03, $01, $03, $01
    .BYTE $03, $03, $01, $03, $01, $03, $01, $01
    .BYTE $03, $01, $03, $01, $01, $01, $01, $01
    .BYTE $02, $01, $01, $01, $02, $01, $01, $01
    .BYTE $02, $01, $01, $01, $01, $01, $01, $01
    .BYTE $01, $01, $01
;---------------------------------------

我需要获取“.BYTE”之后的每个数值,并且对于每 4 个值执行以下操作: - 第一个值不执行任何操作; - 第二个值乘*4; - 第三个值乘*16; - 第四个值乘*64;将这 4 个值相加,并将结果以十六进制格式放入,前缀为“$”,而不是四个原始值。如果“.BYTE”之后的值不是 4 的倍数,或者小于 4,则缺失值应计算为零。例如:

.BYTE $01, $01, $01, $03, $01, $01, $03, $01

应该变成:

.BYTE $57, 5D

因为:

03*1 = 3 +
01*4 = 4 +
01*16 = 16 +
01*64 = 64 = 87 = $57
and
01*1 = 1 +
03*4 = 12 +
01*16 = 16 +
01*64 = 64 = 93 = $5D

未出现的值在数学中应计算为零...显然,不应更改其他文本。

任何人?

答案1

以下sedplus gawk(GNU awk) 解决方案执行请求的任务:

 sed -e '/ .BYTE/{s/\$/0x/g;s/,//g}' INPUTFILE.txt | 
     gawk --non-decimal-data '/.BYTE.*0x/{ printf ".BYTE $%x, $%x\n",
       ($5+$4*4+$3*16+$2*64 ) , ($9+$8*4+$7*16+$6*64) };!/.BYTE.*0x/{print}'

该调用在需要修改的行中sed替换$01为逗号0x01并删除逗号。.BYTE然后 awk 调用识别十六进制数字,执行计算并输出这些行的结果。其他行只需复制到输出即可。

.BYTE该模式适用于给定的示例,但如果使用其他行格式,则可能需要修改。

该命令假定每行恰好有两个 4 个数字的块。所以它输出输入的最后一行

.BYTE $54, $0

相关内容