如何用awk进行十六进制数的计算?
我有一个包含十六进制数字列表的文件,每行一个 0x12345678
。
我想对他们算一笔账。为此,我想到了使用awk。但是,如果使用 printf
函数使用 awk
打印十六进制数字很容易,那么我还没有找到一种方法来将十六进制输入解释为文本(或 0
,到整数的转换在x
处停止)。
awk '{ print $1; }' // 0x12345678
awk '{ printf("%x\n", $1)}' // 0
awk '{ printf("%x\n", $1+1)}' // 1 // DarkDust answer
awk '{ printf("%s: %x\n", $1, $1)}' // 0x12345678: 0
是否可以打印,例如值+1?
awk '{ printf(%x\n", ??????)}' // 0x12345679
编辑:欢迎使用其他语言的衬里! (如果长度合理;-))
I have a file containing a list of hexadecimal numbers, as 0x12345678
one per line.
I want to make a calculation on them. For this, I thought of using awk
. But if printing an hexadecimal number with awk
is easy with the printf
function, I haven't find a way to interpret the hexadecimal input other than as text (or 0
, conversion to integer stops on the x
).
awk '{ print $1; }' // 0x12345678
awk '{ printf("%x\n", $1)}' // 0
awk '{ printf("%x\n", $1+1)}' // 1 // DarkDust answer
awk '{ printf("%s: %x\n", $1, $1)}' // 0x12345678: 0
Is it possible to print, e.g. the value +1?
awk '{ printf(%x\n", ??????)}' // 0x12345679
Edit: One liners on other languages welcomed! (if reasonable length ;-) )
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在原始的
nawk
和mawk
实现中,可以识别十六进制(和八进制)数字。gawk
(我猜你正在使用它)具有不这样做的功能/错误。它有一个命令行开关来获取您想要的行为:--non-decimal-data
。In the original
nawk
andmawk
implementations the hexadecimal (and octal) numbers are recognised.gawk
(which I guess you are using) has the feature/bug of not doing this. It has a command line switch to get the behaviour you want:--non-decimal-data
.gawk 有
strtonum
函数:gawk has the
strtonum
function:也许您根本不需要 awk,因为字符串/数字转换非常复杂。 Bash 版本 3 和 4 非常强大。留在 Bash 中通常更简单、更清晰、更便携,并且可能使用 grep 和 cut 等。
例如,在 Bash 十六进制数字自然转换:
希望这有帮助。
Maybe you don't need awk at all, as string/number conversion is hairy. Bash versions 3 and 4 are very powerful. It is often simpler, clearer and more portable to stay in Bash, and maybe use grep and cut etc.
For example, in Bash hexadecimal numbers are converted naturally:
Hope this helps.
以下是它们行为的不同组合:
使用此命令
gawk -e (GNU Awk 5.1.0, API: 3.0 (GNU MPFR 4.1.0, GNU MP 6.2.1))
gawk -P -e
gawk -c -e
gawk -n -e
gawk -S -e
gawk -M
gawk -l mpfr
nawk (macos awk version 20200816)
mawk 1.3.4
mawk2-beta (1.9.9.6)
事实上,如果有一个自定义 awk 脚本库,可以跨多个 awk 变体工作,但也想考虑它们的特性,一种方法是使用此处输出的差异来自动标记,在需要时留下相对较少的组合决胜局。
here are the different combinations of their behaviors :
using this command
gawk -e (GNU Awk 5.1.0, API: 3.0 (GNU MPFR 4.1.0, GNU MP 6.2.1))
gawk -P -e
gawk -c -e
gawk -n -e
gawk -S -e
gawk -M
gawk -l mpfr
nawk (macos awk version 20200816)
mawk 1.3.4
mawk2-beta (1.9.9.6)
In fact, if one has a custom awk-script library that works across multiple awk variants, but also wanna take their idiosyncrasies into account, one approach would be use the difference in outputs here to auto-flag, with relatively few combinations left where one needs a tie-breaker.
*** 这只是我在 schot 的回复后发表的评论的延伸,严格来说是为了正确的格式化目的。
*** this is only an extension of my comment following schot's response, strictly for proper formatting purposes.