根据我读过的资料, dd 块只是用空格替换换行符。这是正确的还是有其他事情在起作用。
像这样使用时的 unix dd 实用程序:
dd if=foo.log of=bar.log conv=block cbs=2
在这样的文件上:
12
34
56
78
9
IE
12\n34\n56\n78\n9\n
应该给:
12 34 56 78 9
然而它给了
123456789
根据我读过的资料, dd 块只是用空格替换换行符。这是正确的还是有其他事情在起作用。
像这样使用时的 unix dd 实用程序:
dd if=foo.log of=bar.log conv=block cbs=2
在这样的文件上:
12
34
56
78
9
IE
12\n34\n56\n78\n9\n
应该给:
12 34 56 78 9
然而它给了
123456789
那里的文字有点误导。
由于您要求输出记录大小为 2,这正是您得到的。如果换行符尚未超过输出记录大小,它将仅被空格替换。
我认为最好这样说:
对于输入中的每一行,输出“cbs”字节,根据需要用足够的空格替换输入换行符。
我最初认为文档可能只是反映了代码中的工作方式,大致如下:
但事实上,并非如此。最新的dd
源代码有这个(也添加了我自己的评论):
/* Copy NREAD bytes of BUF, doing conv=block
(pad newline-terminated records to `conversion_blocksize',
replacing the newline with trailing spaces). */
static void copy_with_block (char const *buf, size_t nread) {
size_t i;
// For every single character in input buffer.
for (i = nread; i; i--, buf++) {
// If we find a newline.
if (*buf == newline_character) {
// If output record no filled up, pad with spaces.
if (col < conversion_blocksize) {
size_t j;
for (j = col; j < conversion_blocksize; j++)
output_char (space_character);
}
// Regardless, start new output record.
col = 0;
} else {
// No newline.
// If we hit output limit, increment truncated-lines count.
// Otherwise only output character if under limit.
if (col == conversion_blocksize)
r_truncate++;
else
if (col < conversion_blocksize)
output_char (*buf);
// Regardless, increment characters-on-this-line count.
col++;
}
}
}
在此,您清楚地使用全局来一次处理一个字符col
来存储您的输出列。它明确指出,一旦您在输入流中找到换行符,它就会被替换为最大转换块大小的空格。
如果在达到转换块大小之前没有找到换行符,则所有其他字符都将被丢弃,直到并包括下一个换行符。