以十六进制格式每行一个字节的输出文件(在 linux bash 下)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21713725/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 09:32:17  来源:igfitidea点击:

Output file with one byte per line in hex format (under linux bash)

bashoutputhexdump

提问by Oliver R.

As the title says, here is an example:

正如标题所说,这是一个例子:

$ cat test.txt 
ABCD
$ hd test.txt 
00000000  41 42 43 44 0a                                    |ABCD.|
00000005

my desired output would be:

我想要的输出是:

41
42
43
44

I know that this is possible with sed, awk and stuff, but this might be very slow for large files. I thought of an format string for "hexdump" or a parameter combination for "od". Could you please help me out?

我知道这对于 sed、awk 和其他东西是可能的,但是对于大文件来说这可能会很慢。我想到了“hexdump”的格式字符串或“od”的参数组合。你能帮我一下吗?

回答by John Kugelman

$ echo -n 'ABCD' | hexdump -v -e '/1 "%02x\n"'
41
42
43
44
0a

If you don't want it to print 0aat the end for the newline then I'd recommend getting rid of the newline from the file first.

如果您不希望它0a在换行符的末尾打印,那么我建议您首先从文件中删除换行符。

回答by Walter Tross

od -An -vtx1 -w1 test.txt | cut -c2-

If you don't want newlines:

如果您不想要换行符:

od -An -vtx1 -w1 test.txt | cut -c2- | fgrep -v 0a

Meaning of odoptions:

od选项的含义:

  • -An: addresses: no
  • -v: verbose, i.e., don't write a *instead of repeating lines
  • -tx1: output type: hex, 1 byte (use -tx1zfor an hd-like output)
  • -w1: max width: 1
  • -An: 地址: 没有
  • -v: 冗长,即不要写 a*而不是重复行
  • -tx1: 输出类型:十六进制,1 字节(-tx1z用于类似hd输出)
  • -w1:最大宽度:1

One nice thing about odis that it's available on any *xsystem.

一件好事od是它可以在任何*x系统上使用。