作为问题本身,我不知道为什么会tail那样做。我有一个名为的文件myfile.txt,其内容是:
firstline
secondline
thirdline
Run Code Online (Sandbox Code Playgroud)
所以当我使用:
tail -c-1 myfile.txt
Run Code Online (Sandbox Code Playgroud)
或者
tail -c+1 myfile.txt
Run Code Online (Sandbox Code Playgroud)
它输出:
firstline
secondline
thirdline
Run Code Online (Sandbox Code Playgroud)
man tail:
-c, --bytes = [+] NUM
输出最后 NUM 个字节;或使用 -c +NUM 以每个文件的字节 NUM 开头输出
tail -c+1 myfile.txt是一样的cat myfile.txt:你告诉tail与开始输出第一(+1)字节(-c),换句话说:在整个文件。
tail -c-1 myfile.txt(更典型地为:tail -c1 myfile.txt)仅输出的最后一个字节在myfile.txt。
假设这myfile.txt是一个格式正确的文本文件,以尾随结尾\n,并使用单字节编码(例如 ASCII)或将单字节 ASCII 编码作为子集的编码(例如 UTF-8),这将仅输出\n,即,一个空行。
将tail的基本逻辑概括为(涵盖GNU和BSD/macOS实现):
注意:
-tail支持此处未讨论的其他选项 - 请参阅man 1 tail。
- 在以下所有形式中,选项 ( ) 与其选项参数 ( /或)之间的空格是可选的- 这通常适用于与 POSIX 兼容的实用程序。-<units><count>-<option>+<index>
# Output <count> units *from the end* of the input.
# The following two forms are equivalent.
tail -<units> <count>
tail -<units> -<count>
# This next form is equivalent to: tail -n <line-count>.
tail -<line-count>
# Output everything *starting from* 1-based unit index <index>.
# In other words: Skip <index> - 1 units at the *start* of the input.
tail -<units> +<index>
Run Code Online (Sandbox Code Playgroud)
Option-<units>指定选项参数/的单位。<count><index>
-n指的是线
-<units>和仅使用-<count>- 注意所需的-前缀 - 与-n <count>. (BSD/macOStail也支持+<count>隐含形式-n +<count>,但 GNUtail不支持。)-c指的是字节(!),是不是UTF8感知在任一执行。tail还支持-b512 字节的块。如果<count>没有符号(例如,1)或显式减号(例如,-1; 永远不需要),则从输入的末尾<count>返回单位。
甲+-prefixed值被取为1基于(!)单元指数从开始输入从其中输出的输入线路。换句话说:+<index>意思是:跳过<index> - 1开头的单元,输出其余的所有单元;例如,tail -n +2输出从 - 并包括-第二行开始的所有内容。
既省略-<units>和<count>/ -<count>/+<index>是一样的tail -n 10,也就是说tail的默认行为是输出输入的最后 10 行。
如果这个逻辑应用到OP的后续问题有关的行为-c+0和-c-0:
-c+0被同等对待-c+1,因此输出的整个输入(如相同cat):你问的一切起始于“零”字节,它不存在,但由于0 < 1与1作为第一实际字节位置,你仍然可以得到整个输入作为输出。
-c-0根本不输出任何内容,因为您要求从输入的末尾返回零字节(换句话说:nothing)。