将文本文件中的十六进制值转换为十进制

convert hex values to decimal in a text file

我有一个如下所示的文本文件:

RAM_SIZE 3128
RAM_ADDRESS_WIDTH 0xF
MTFE 0xF
IPS_ADDR_WIDTH 314

我想将十六进制值转换为十进制并显示如下内容:

RAM_SIZE 3128
RAM_ADDRESS_WIDTH 15
MTFE 15
IPS_ADDR_WIDTH 314

我试过 awk:

#!/bin/awk -f
{
  if ( == "0x*")
    printf "%s %d \n", ,  ;
  else 
    print  " "  
}

在 if 情况下无法正确使用通配符。

我也想使用 sed,但我不知道如何在 sed 表达式中使用列号:

sed -e 's/0x*/$(())/' 

使用 gnu-awk 非常简单:

awk '{print , strtonum()}' file
RAM_SIZE 3128
RAM_ADDRESS_WIDTH 15
MTFE 15
IPS_ADDR_WIDTH 314

-n 告诉 GNU awk 处理非十进制数据:

$ gawk -n '{+=0}1' file        
RAM_SIZE 3128
RAM_ADDRESS_WIDTH 15
MTFE 15
IPS_ADDR_WIDTH 314

根据你的声明 Not able to use wildcards properly in if case.,你写道:

if ( == "0x*")

哪里

"0x*" is a STRING containing the characters "0", "x", and "*".
== is the STRING comparison operator.

虽然你想要的是一个正则表达式比较,而不是一个字符串比较,所以你应该开始写:

if ( ~ /0x*/)

因为:

/0x*/ is a REGEXP that means `0 then x repeated zero or more times`
~ is the REGEXP comparison operator

但我怀疑您并不是真的想要 * 而您实际想要的是:

if ( ~ /^0x/)

因为:

/^0x/ is a REGEXP that means `starting with 0 and followed by x`