我有一个名为samples.txt的文件,其中有374371行,创建如下:
hexdump -e '8/1 "%02x " "n"' samples.bin > samples.txt
它看起来像这样(不推荐使用(:
1a 03 1a 03 4a 03 57 03
4b 03 44 03 1e 03 09 04
10 03 19 03 40 03 ae 03
1e 03 26 03 33 03 ad 03
10 03 84 03 43 03 62 03
e0 03 16 03 34 03 c4 03
f8 02 3b 03 53 03 61 03
10 03 15 03 42 03 58 03
23 03 1f 03 57 03 62 03
17 03 73 03 31 03 33 03
14 03 ff 02 30 03 51 03
5f 03 42 03 47 03 7e 03
ba 03 26 03 35 03 a0 03
08 03 33 03 36 03 c8 03
0c 03 38 03 97 03 44 03
和以下sed命令:
echo " ch0 ch1 ch2 ch3"; cat -n samples.txt | sed 's/s*([0-9]*)s*([0-9a-f]*) ([0-9a-f]*) ([0-9a-f]*) ([0-9a-f]*) ([0-9a-f]*) ([0-9a-f]*) ([0-9a-f]*) ([0-9a-f]*)/Sample 1:t0x23 0x45 0x67 0x89/' | head -n 15
给我的输出看起来像这样:
ch0 ch1 ch2 ch3
Sample 1: 0x1a03 0x1a03 0x4a03 0x5703
Sample 2: 0x4b03 0x4403 0x1e03 0x0904
Sample 3: 0x1003 0x1903 0x4003 0xae03
Sample 4: 0x1e03 0x2603 0x3303 0xad03
Sample 5: 0x1003 0x8403 0x4303 0x6203
Sample 6: 0xe003 0x1603 0x3403 0xc403
Sample 7: 0xf802 0x3b03 0x5303 0x6103
Sample 8: 0x1003 0x1503 0x4203 0x5803
Sample 9: 0x2303 0x1f03 0x5703 0x6203
Sample 10: 0x1703 0x7303 0x3103 0x3303
Sample 11: 0x1403 0xff02 0x3003 0x5103
Sample 12: 0x5f03 0x4203 0x4703 0x7e03
Sample 13: 0xba03 0x2603 0x3503 0xa003
Sample 14: 0x0803 0x3303 0x3603 0xc803
Sample 15: 0x0c03 0x3803 0x9703 0x4403
在我看来,将hexdump的输出重定向到临时samples.txt文件是低效的,因为对于一个文件,你必须在最后删除它,对于两个文件,也许在内存中的数据上使用sed比在磁盘上更快。如何跳过samples.txt文件并获得相同的结果?
如果我正确理解您的意图,我认为以下内容应该有效。
hexdump -e '8/1 "%02x " "n"' samples.bin | cat -n | sed '....
具有xxd
和coreutils的替代方案:
{
# Print header
printf "ttCh0 Ch1 Ch2 Ch3n"
# Create hexdump of data
xxd -p samples.bin |
# Use 16 bit samples
fold -w4 |
# Add hex indicator
sed 's/^/0x/' |
# Data contains 4 interleaved channels
xargs -n4 |
# Number each sample
nl -n ln |
sed 's/ /:/; s/^/Sample /'
} > samples.out
samples.out
:的内容
Ch0 Ch1 Ch2 Ch3
Sample 1: 0x1a03 0x1a03 0x4a03 0x5703
Sample 2: 0x4b03 0x4403 0x1e03 0x0904
Sample 3: 0x1003 0x1903 0x4003 0xae03
Sample 4: 0x1e03 0x2603 0x3303 0xad03
Sample 5: 0x1003 0x8403 0x4303 0x6203
Sample 6: 0xe003 0x1603 0x3403 0xc403
Sample 7: 0xf802 0x3b03 0x5303 0x6103
Sample 8: 0x1003 0x1503 0x4203 0x5803
Sample 9: 0x2303 0x1f03 0x5703 0x6203
Sample 10: 0x1703 0x7303 0x3103 0x3303
Sample 11: 0x1403 0xff02 0x3003 0x5103
Sample 12: 0x5f03 0x4203 0x4703 0x7e03
Sample 13: 0xba03 0x2603 0x3503 0xa003
Sample 14: 0x0803 0x3303 0x3603 0xc803
Sample 15: 0x0c0a 0x3803 0x9703 0x4403