使用awk或sed将十六进制转换为十进制 [英] Converting hexadecimal to decimal using awk or sed

查看:139
本文介绍了使用awk或sed将十六进制转换为十进制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的数据,以逗号分隔:

I have a data like this, with comma-separated:

18:22:05,OtherOMoperation,BatuAmpar,BatuAmparMG1,2fe3,c7b1
18:22:05,OtherOMoperation,BatuAmpar,BatuAmparMG2,2fe3,c7b2
18:22:05,OtherOMoperation,BatuAmpar,BatuAmparMG3,2fe3,c7b3
03:25:55,Othercauses,N_Pancamukti,N_PancamuktiMG1,2f50,c775
03:25:55,Othercauses,N_Pancamukti,N_PancamuktiMG2,2f50,c776

我需要将最后两列转换为十进制,如下所示:

I need to convert last two columns into decimal as below:

18:22:05,OtherOMoperation,BatuAmpar,BatuAmparMG1,12259,51121
18:22:05,OtherOMoperation,BatuAmpar,BatuAmparMG2,12259,51122
18:22:05,OtherOMoperation,BatuAmpar,BatuAmparMG3,12259,51123
03:25:55,Othercauses,N_Pancamukti,N_PancamuktiMG1,12112,51061
03:25:55,Othercauses,N_Pancamukti,N_PancamuktiMG2,12112,51062

我已经尝试过这个了:
cat | nawk -F,'{printf%d \ n",$ 1}"

I have already try this one :
cat |nawk -F, '{printf "%d\n" , $1}'

除零外,我什么也没得到:
0 0 0 0 0 0 0 0 0

I get nothing but zeroes as output:
0 0 0 0 0 0 0 0 0

推荐答案

使用Perl:

perl -lpe 's/([^,]*),([^,]*)$/hex($1).",".hex($2)/e' input

输出:

18:22:05,OtherOMoperation,BatuAmpar,BatuAmparMG1,12259,51121
18:22:05,OtherOMoperation,BatuAmpar,BatuAmparMG2,12259,51122
18:22:05,OtherOMoperation,BatuAmpar,BatuAmparMG3,12259,51123
03:25:55,Othercauses,N_Pancamukti,N_PancamuktiMG1,12112,51061
03:25:55,Othercauses,N_Pancamukti,N_PancamuktiMG2,12112,51062

和gawk中的类似:

gawk --non-decimal-data -F, '
      BEGIN{OFS=FS}{for(i=5;i<7;i++) $i=sprintf("%d","0x"$i)}1' input

这篇关于使用awk或sed将十六进制转换为十进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆