可以打印f“%x \ n" \'以awk执行吗? [英] Can printf "%x\n" \'a be performed in awk?

查看:139
本文介绍了可以打印f“%x \ n" \'以awk执行吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所有可打印字符的十六进制代码值都可以用bash显示.

All printable characters' hex code values can be displayed this way in bash.

printf "%x\n"  \'a
61

awk 'BEGIN{printf("%x\n",\\'a)}'
awk 'BEGIN{printf("%x\n",\'a)}'

它们都不可以在awk中执行,没有办法在awk中执行吗?
awk不提供bash这样的printf格式吗?

None of them can be performed in awk,is there no way to do in awk?
awk doesn't provide this kind of printf format such as in bash?

awk -v var="a"  'BEGIN{printf("%x\n", var)}'
0
echo -n  a|xxd
0000000: 61   

使用 echo -na | xxd 获取 a 可打印字符的十六进制代码值很简单,我的问题是请问awk是否提供这种printf格式例如是否在bash中,而不是在awk中如何使用其他方法获取十六进制代码值.

It is simple to get the a printable characters' hex code value with echo -n a|xxd,my question is to ask does awk provide this kind of printf format such as in bash or not ,not about how to get the hex code value with other method in awk.

awk -v var="a"  'BEGIN{printf("%x\n", \'var)}'
bash: syntax error near unexpected token `)'
debian8@debian:~$ awk -v var="a"  "BEGIN{printf("%x\n", \'var)}"
awk: cmd. line:1: BEGIN{printf(%xn, \'var)}
awk: cmd. line:1:              ^ syntax error
awk: cmd. line:1: BEGIN{printf(%xn, \'var)}
awk: cmd. line:1:                   ^ backslash not last character on line
awk: cmd. line:1: BEGIN{printf(%xn, \'var)}
awk: cmd. line:1:                   ^ syntax error

结论:awk不支持这种printf格式.

Conclusion:awk doesn't support this kind of printf format.

推荐答案

以下命令显示 awkprintf函数确实不支持前缀以获取字符的代码点(适用于GNU Awk,Mawk和BSD/macOS Awk):

Here's a command that shows that awk's printf function indeed does not support the '-prefixed syntax for getting a character's code point (applies to GNU Awk, Mawk, and BSD/macOS Awk):

$ awk -v char="'a" 'BEGIN { printf "%x\n", char }'
0  # Value 'a is literally interpreted as a number, which defaults to 0

请注意, Bash v4 +的内置printf支持Unicode :

Note that Bash v4+'s printf builtin is Unicode-aware:

$ printf '%x\n' \'€
20ac  # U+20AC is the Unicode code point of the EURO symbol

十六进制转储实用程序(例如xxd)只会为您提供字符的 byte 表示形式,而仅与相同>代码点在7位ASCII范围内.
在基于UTF-8的语言环境中(这是如今的典型情况),超出ASCII范围的任何内容都会打印出构成字符的UTF-8编码形式的字节:

A hex-dump utility such as xxd will only give you the byte representation of a character, which is only the same as the code point in the 7-bit ASCII range.
In a UTF-8-based locale (which is typical these days), anything beyond the ASCII range will print the bytes that make up the UTF-8-encoded form of the character:

$ xxd <<<€
00000000: e282 ac0a # 0xe2 0x82 0xac are the UTF-8 encoding of Unicode char. U+20AC

Ed Morton的帮助与 GNU Awk 一起使用的 ord()函数答案 仅限ASCII字符.代码点超过0x7f的任何字符都会导致值.

The ord() function used with GNU Awk in Ed Morton's helpful answer is limited to ASCII characters. Any character with a codepoint beyond 0x7f results in a negative value.

创建所有字符映射图,来自 James Brown的有用答案:

  • 限于 Mawk BSD/macOS Awk

原则上可以使用 GNU Awk 中的所有Unicode字符,但是必须构建所有字符的映射这一事实使这有点不切实际;这是一个涵盖Unicode BMP(基本多语言平面)的版本,最常用的字符会落在哪个位置.

in principle works with all Unicode characters in GNU Awk, but the fact that a map of all characters must be built makes this somewhat impractical; here's a version that covers the Unicode BMP (basic multilingual plane), into which the most widely used characters fall.

$ gawk -v char=€ 'BEGIN{ for(n=0;n<=0xffff;n++) ord[sprintf("%c",n)]=n; printf "%x\n", ord[char]}'
20ac

这篇关于可以打印f“%x \ n" \'以awk执行吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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