gpt4 book ai didi

shell - 使用 awk 或 sed 将十六进制转换为十进制

转载 作者:行者123 更新时间:2023-12-04 04:54:39 28 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





Converting hex to decimal in awk or sed

(7 个回答)


8年前关闭。




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

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

我需要将最后两列转换为十进制,如下所示:
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}'

我只得到零作为输出:
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

关于shell - 使用 awk 或 sed 将十六进制转换为十进制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16934153/

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