2015-12-07 75 views
1

我想从hexdump以下结果包围:hexdump都可以转换字符串由转义字符格式字符串

78  79  7a 

这是"\t78\t\t79\t\t7a\t"

试图

echo -n xyz | hexdump -e '1/1 "\t%x\t"' 

导致错误:

hexdump: % : bad conversion character 

echo -n xyz | hexdump -e '1/1 "|%x|"' 

正确产生

|78||79||7a| 

增加空格:

echo -n xyz | hexdump -e '1/1 "\t %x \t"' 

东西

t 78  t 79  t 7a  

这是"\tt 78\t\tt 79\t\tt 7a\t"但我得到两个所需的选项卡文字字母t加上一些不需要的空格字符。

只使用一个单一的尾随标签

echo -n xyz | hexdump -e '1/1 "%x\t"' 

时给我

78 79 7a 

这是"78\t79\t7a\t"而不是一个单一的主导标签

echo -n xyz | hexdump -e '1/1 "\t%x"' 

这给了我另一个它的工作原理错误

hexdump: %A: bad conversion character 

我不知道哪里的错误是从,因为没有任何地方%A未来。

根据手册页,\t应该是一个支持的转义序列,我把它当作printf中的任何其他字符对待。

The format is required and must be surrounded by double quote (" ") marks. It is interpreted as a fprintf-style format string (see fprintf(3)), with the following exceptions:

+o An asterisk (*) may not be used as a field width or precision. 

+o A byte count or field precision is required for each ``s'' con- 
    version character (unlike the fprintf(3) default which prints 
    the entire string if the precision is unspecified). 

+o The conversion characters ``h'', ``l'', ``n'', ``p'' and ``q'' 
    are not supported. 

+o The single character escape sequences described in the C stan- 
    dard are supported: 

     NUL     \0 
     <alert character> \a 
     <backspace>   \b 
     <form-feed>   \f 
     <newline>   \n 
     <carriage return> \r 
     <tab>    \t 
     <vertical tab>  \v 
+0

是否有可能你的shell会首先尝试扩大单反斜线?即,尝试使用双反斜杠。 – usr2564301

+2

导致'\\ t'的错误。使用bash和整个arg是在一个单引号字符,因此*不应*做壳侧的任何扩展。 – arcyqwerty

+1

'hexdump:%\:错误的转换字符'同时使用'\\ t%x \\ t' – arcyqwerty

回答

2

此行为is actually a fixed, not so long ago, bug。对于受影响的版本,有一种解决方法:只需将前导反斜杠放入单独的格式字符串中即可。

例如,你想要的代码是这样:

echo -n xyz | hexdump -e '"\t" 1/1 "%x"'