linux cat tac
The cat
and tac
commands display the contents of text files, but there’s more to them than meets the eye. Dive a little deeper and learn some productive Linux command line tricks.
cat
和tac
命令显示文本文件的内容,但是它们不仅仅具有视觉效果。 深入一点,学习一些有用Linux命令行技巧。
These are two simple little commands, often dismissed as being just that—too simple to be of any real use. But once you know the different ways you can use them, you’ll see that they are perfectly capable of doing their fair share of the heavy lifting when it comes to working with files.
这是两个简单的小命令,通常只是这样而忽略不了-太简单了以至于没有任何实际用途。 但是一旦您知道了使用它们的不同方式,您就会发现它们完全有能力在处理文件时尽其所能。
猫命令 (The cat Command)
cat
is used to examine the contents of text files, and to join parts of files together to form a larger file.
cat
用于检查文本文件的内容,并将文件的各个部分连接在一起以形成更大的文件。
At one time—back in the era of the dial-up modem—binary files were often broken into several smaller files to make downloading easier. Instead of downloading one large file, you pulled back each smaller file. If a single file failed to download correctly, you would just retrieve that one file again.
一次(在拨号调制解调器时代),二进制文件经常被分解成几个较小的文件,以使下载变得更容易。 您无需下载一个大文件,而是撤回了每个小文件。 如果单个文件无法正确下载,则只需再次检索该文件。
Of course, you then needed a way to reconstitute the collection of smaller files back into the single working binary file. That process was called concatenating. And that’s where cat
came in and where it gets its name from.
当然,您然后需要一种方法来将较小文件的集合重新构造回单个工作的二进制文件。 该过程称为串联。 那就是cat
进来的地方, cat
也因此而得名。
Broadband and fiber connections have caused that particular need to fade—much like screechy dial-ups sounds—so what’s left for cat
to do today? Quite a lot actually.
宽带和光纤连接造成的特定需要淡出很像惨笑拨号窗口的声音,所以还剩下些什么的cat
今天做? 实际上很多。
显示文本文件 (Displaying a Text File)
To have cat
list the contents of a text file to a terminal window, use the following command.
要让cat
在终端窗口中列出文本文件的内容,请使用以下命令。
Make sure the file is a text file. If you try to list the contents of a binary file to the terminal window, the results will be unpredictable. You might end up with a locked terminal session or worse.
确保该文件是文本文件。 如果您尝试将二进制文件的内容列出到终端窗口中,结果将是不可预测的。 您可能会遇到终端会话锁定或更糟的情况。
cat poem1.txt
The contents of the file poem1.txt are shown in the terminal window.
终端窗口中显示文件poem1.txt的内容。
That’s only half of the famous poem. Where’s the rest of it? There ‘s another file here called poem2.txt. We can make cat
list the contents of multiple files with one command. All we need to do is list the files in order on the command line.
那只是那首著名诗的一半。 其余的在哪里? 这里还有另一个文件poem2.txt。 我们可以用一个命令使cat
列出多个文件的内容。 我们需要做的就是在命令行中按顺序列出文件。
cat poem1.txt poem2.txt
That looks better; we have the whole poem now.
看起来更好; 我们现在有整首诗。
少用猫 (Using cat With less)
The poem is all there, but it shot past the window too fast to read the first few verses. We can pipe the output from cat
into less
and scroll down through the text at our own pace.
这首诗就在那儿,但它从窗外射出的速度太快,无法阅读前几节经文。 我们可以将cat
的输出输出到less
并按照自己的速度向下滚动文本。
cat poem1.txt poem2.txt | less
We can now move backward and forward through the text in one stream, even though it is held in two separate text files.
现在,即使它被保存在两个单独的文本文件中,我们也可以在一个流中前后移动文本。
对文件中的行编号 (Numbering the Lines in a File)
We can have cat number the lines in the file as it is displayed. To do this, we use the -n
(number) option.
我们可以让目录号显示文件中的行数。 为此,我们使用-n
(数字)选项。
cat -n poem1.txt
The lines are numbered as they are displayed in the terminal window.
这些行按照在终端窗口中显示的方式编号。
不要为空行编号 (Don’t Number Blank Lines)
We managed to have the lines numbered by cat
, but the blank lines between the verses are being counted as well. To have the text lines numbered but to ignore the blank lines, use the -b
(number-nonblank) option.
我们设法使行用cat
编号,但是经文之间的空行也被计算在内。 要对文本行进行编号,但忽略空白行,请使用-b
(数字-非空白)选项。
cat -b poem1.txt
Now the text lines are numbered, and the blanks lines are skipped.
现在,文本行已编号,空白行已跳过。
不要显示多个空行 (Don’t Show Multiple Blank Lines)
If there are sections of consecutive blank lines in a file, we can ask cat
to ignore all but one blank line. Look at this file.
如果文件中有连续的空白行部分,我们可以要求cat
忽略除一个空白行以外的所有行。 看这个文件。
The next command will cause cat
to display only one blank line from each bunch of blank lines. The option we need to achieve this is the -s
(squeeze-blank) option.
下一条命令将使cat
在每行空白行中仅显示一个空白行。 我们需要实现的选项是-s
(squeeze-blank)选项。
cat -s poem1.txt
This doesn’t affect the contents of the file in any way; it just changes the way cat
displays the file.
这不会以任何方式影响文件的内容。 它只是改变了cat
显示文件的方式。
显示标签 (Display Tabs)
If you want to know whether whitespace is caused by spaces or tabs, you can find out using the -T
(show-tabs) option.
如果您想知道空格是由空格还是制表符引起的,则可以使用-T
(显示选项卡)选项进行查找。
cat -T poem1.txt
The tabs are represented by the characters “^I”.
这些选项卡由字符“ ^ I”表示。
显示行尾 (Displaying the Ends of Lines)
You can check for trailing whitespace by using the -E
(show-ends) option.
您可以使用-E
(show-ends)选项检查尾随空格。
cat -E poem1.txt
The ends of lines are represented by the “$” character.
行尾由“ $”字符表示。
串联文件 (Concatenating Files)
It doesn’t make sense to have a poem saved in two files, with one half in each. Let’s join them together and make a new file with the entire poem in it.
将一首诗保存在两个文件中,每个文件各占一半是没有意义的。 让我们将它们组合在一起,并在其中包含整首诗的情况下制作一个新文件。
cat poem1.txt poem2.txt > jabberwocky.txt
let’s use cat
to check our new file:
让我们使用cat
检查我们的新文件:
cat jabberwocky.txt
Our new file contains the contents of the other two files.
我们的新文件包含其他两个文件的内容。
将文本追加到现有文件 (Appending Text to an Existing File)
That’s better, but in actual fact, it’s not the entire poem. The last verse is missing. The last verse in Jabberwocky is the same as the first verse.
更好,但实际上,这不是整首诗。 最后一节经文缺失。 Jabberwocky中的最后一节与第一节相同。
If we’ve got the first verse in a file, we can add this to the bottom of the jabberwocky.txt file, and we’ll have the complete poem.
如果文件中有第一节经文,则可以将其添加到jabberwocky.txt文件的底部,我们将拥有一首完整的诗。
In this next command, we have to use >>
, not just >
. If we use a single >
we’ll overwrite jabberwocky.txt. We don’t want to do that. We want to append text to the bottom of it.
在下一个命令中,我们必须使用>>
,而不仅仅是>
。 如果使用单个>
我们将覆盖jabberwocky.txt。 我们不想这样做。 我们想在其底部附加文本。
cat first_verse.txt >> jabberwocky.txt
Let’s check the contents of the jabberwocky.txt file:
让我们检查一下jabberwocky.txt文件的内容:
cat jabberwocky.txt
And finally, all the parts of the poem are together.
最后,这首诗的所有部分都在一起。
重定向标准输入 (Redirecting stdin)
You can redirect input from the keyboard into a file using cat
. Everything you type is redirected into the file until you hit Ctrl+D. Note that we use a single >
because we want to create the file (or overwrite it, if it exists).
您可以使用cat
将键盘上的输入重定向到文件中。 您键入的所有内容都会重定向到文件中,直到您按Ctrl + D。 请注意,我们使用单个>
因为我们要创建文件(或覆盖文件(如果存在))。
cat > my_poem.txt
We can start typing as soon as we issue the command. We hit Ctrl+D when we’ve finished. We can then check the contents of the new file with:
发出命令后,我们便可以开始键入。 完成后,我们按Ctrl + D。 然后,我们可以使用以下命令检查新文件的内容:
cat my-poem.txt
That sound like a far-off turbine is probably Lewis Carroll spinning in his grave at high speed.
听起来像是遥远的涡轮机,可能是刘易斯·卡洛尔(Lewis Carroll)在自己的坟墓中高速旋转。
tac命令 (The tac Command)
tac
is similar to cat
, but it lists the contents of files in reverse order.
tac
与cat
类似,但是它以相反的顺序列出文件的内容。
Let’s see that:
让我们看看:
tac my_poem.txt
And the file is listed to the terminal window in reverse order. In this case, it has no effect on its literary merits.
并且文件以相反的顺序列出到终端窗口中。 在这种情况下,它不会影响其文学价值。
与tdin一起使用tac (Using tac With stdin)
Using tac
without a filename will cause it to operate on the input from the keyboard. Hitting Ctrl+D will stop the input phase, and tac will list in reverse order whatever you’d typed in.
使用不带文件名的tac
会使它在键盘输入上进行操作。 按Ctrl + D将停止输入阶段,而tac将以相反的顺序列出您键入的内容。
tac
When Ctrl+D is hit, the input is reversed and listed to the terminal window.
当按Ctrl + D组合键时,输入将反转并显示在终端窗口中。
在日志文件中使用tac (Using tac With Log Files)
Apart from low-grade parlor tricks, can tac
do anything useful? Yes, it can. Many log files append their newest entries at the bottom of the file. Using tac
(and, counterintuitively, head
) we can pop the last entry into the terminal window.
除了低级客厅技巧之外, tac
还能做些有用的事情吗? 是的,它可以。 许多日志文件将其最新条目附加在文件底部。 使用tac
(和直觉上相反,是head
),我们可以将最后一个条目弹出到终端窗口中。
We use tac
to list the syslog file in reverse, and pipe it into head
. By telling head
to only print the first line it receives (which thanks to tac
is the last line in the file), we see the latest entry in the syslog file.
我们使用tac
反向列出syslog文件,并将其通过管道传递到head
。 通过告诉head
只打印接收到的第一行(由于tac
是文件中的最后一行),我们在syslog文件中看到了最新的条目。
tac /var/log/syslog | head -1
head
prints the latest entry from the syslog file and then exits.
head
打印syslog文件中的最新条目,然后退出。
Note that head
is only printing one line—as we requested—but the line is so long it wraps around twice. That’s why it looks like three lines of output in the terminal window.
请注意,根据我们的要求, head
只打印一行,但是该行太长了,因此只能绕两次。 这就是在终端窗口中看起来像三行输出的原因。
在文本记录中使用tac (Using tac with Text Records)
The last trick tac
has up its sleeve is a beauty.
tac
的最后一个绝招是美丽。
Usually, tac
operates on text files by working its way through them line by line, from the bottom up. A line is a sequence of characters terminated by a newline character. But we can tell tac
to work with other delimiters. This allows us to treat “chunks” of data within the text file as data records.
通常, tac
通过自下而上逐行处理文本文件来处理文本文件。 行是由换行符终止的字符序列。 但是我们可以告诉tac
与其他定界符一起使用。 这使我们可以将文本文件中的数据“块”视为数据记录。
Let’s say we have a log file from some program that we need to review or analyze. Let’s have a look at its format with less
.
假设我们有一些程序的日志文件,我们需要对其进行检查或分析。 我们来看一下less
格式。
less logfile.dat
As we can see, there is a repeating format to the file. There are sequences of three lines of hexadecimal values. Each set of three lines of hexadecimal has a label line that starts “=SEQ”, followed by a sequence of digits.
如我们所见,文件有重复格式。 由三行十六进制值组成的序列。 每组三行十六进制都有一个以“ = SEQ”开头的标签行,后跟一个数字序列。
If we scroll to the bottom of the file, we can see that there are a lot of these records. The final one is numbered 865.
如果我们滚动到文件的底部,我们可以看到有很多这样的记录。 最后一个编号为865。
Let’s assume that for whatever reason we need to work through this file in reverse order, data record by data record. The line order of the three hexadecimal lines in each data record must be preserved.
假设出于任何原因,我们都需要以相反的顺序遍历该文件,逐条记录数据。 必须保留每个数据记录中三个十六进制行的行顺序。
We’ll make a note that the final three lines in the file start with hexadecimal values 93, E7 and B8, in that order.
我们将注意文件中的最后三行以十六进制值93,E7和B8依次开始。
Let’s use tac
to reverse the file. It is a very long file so we’ll pipe it into less
.
让我们使用tac
来反转文件。 这是一个很长的文件,所以我们用less
。
tac logfile.dat | less
That reverses the file, but it isn’t the result we want. We want the file to be reversed, but the lines in each data record must be in their original order.
那会反转文件,但这不是我们想要的结果。 我们希望反转文件,但是每个数据记录中的行必须保持原始顺序。
We recorded earlier that the final three lines in the file start with hexadecimal values 93, E7 and B8, in that order. The order of those lines has been reversed. Also, the “=SEQ” lines are now below each set of three hexadecimal lines.
我们之前记录了文件的最后三行以十六进制值93,E7和B8开头。 这些行的顺序已颠倒。 同样,“ = SEQ”行现在位于每组三个十六进制行的下方。
tac
to the rescue.
tac
进行救援。
tac -b -r -s ^=SEQ.+[0-9]+*$ logfile.dat | less
Let’s break that down.
让我们分解一下。
The -s
(separator) option informs tac
what we want to use as the delimiter between our records. It tells tac
not to use its usual newline character, but to use our separator instead.
-s
(分隔符)选项通知tac
我们想要用作记录之间定界符的内容。 它告诉tac
不要使用其通常的换行符,而应使用分隔符。
The -r
(regex) option tells tac
to treat the separator string as a regular expression.
-r
(regex)选项告诉tac
将分隔符字符串视为正则表达式。
The -b
(before) option causes tac
to list the separator before each record instead of after it (which is the usual position of its default separator, the newline character).
-b
(之前)选项使tac
在每个记录之前而不是之后列出分隔符(这是其默认分隔符的新位置,换行符)。
The -s
(separator) string ^=SEQ.+[0-9]+*$
is deciphered as follows:
-s
(分隔符)字符串^=SEQ.+[0-9]+*$
的解密方式如下:
The ^
character represents the start of the line. This is followed by =SEQ.+[0-9]+*$
. This instructs tac
to look for each occurrence of “=SEQ.” at the start of a line, followed by any sequence of digits (indicated by [0-9]
), and followed by any other set of characters (indicated by *$
).
^
字符代表行的开始。 随后是=SEQ.+[0-9]+*$
。 这指示tac
查找每次出现的“ = SEQ”。 在一行的开头,后面是任意数字序列(由[0-9]
表示),然后是任何其他字符集(由*$
表示)。
We’re piping the whole lot into less
, as usual.
像往常一样,我们将整个批次less
到less
。
Our file is now presented in reverse order with each “=SEQ” label line listed before its three lines of hexadecimal data. The three lines of hexadecimal values are in their original order within each data record.
现在,我们的文件以相反的顺序显示,每条“ = SEQ”标签行在其三行十六进制数据之前列出。 在每个数据记录中,三行十六进制值按其原始顺序排列。
We can check this simply. The first value of the first three lines of hexadecimal (which were the last three lines before the file was reversed) match the values that we took a record of earlier: 93, E7 and B8, in that order.
我们可以简单地检查一下。 十六进制的前三行的第一个值(即文件反转之前的最后三行)与我们之前记录的值相匹配:93,E7和B8(按此顺序)。
That’s quite a trick for a terminal window one-liner.
对于终端窗口单线而言,这是一个绝招。
一切都有目的 (Everything Has a Purpose)
In the Linux world, even the seemingly simplest commands and utilities can have surprising and powerful properties.
在Linux世界中,即使看似最简单的命令和实用程序也可以具有令人惊讶且强大的属性。
The design philosophy of simple utilities that do one thing well, and which easily interwork with other utilities, has given rise to some strange little commands, such as tac
. At first glance, it appears to be a bit of an oddity. But when you peer beneath the surface, there is an unexpected power that you can leverage to your advantage.
简单实用程序的设计原理可以很好地完成一件事情,并且可以轻松地与其他实用程序交互工作,从而产生了一些奇怪的小命令,例如tac
。 乍一看,这似乎有些奇怪。 但是,当您窥视表面之下时,您会拥有出乎意料的力量,可以发挥自己的优势。
Or, as another philosophy says, “Do not despise the snake for having no horns, for who is to say it shall not become a dragon?”
或者,就像另一种哲学所说的那样:“不要鄙视没有角的蛇,因为谁会说它不会变成龙?”
翻译自: https://www.howtogeek.com/424234/how-to-use-the-linux-cat-and-tac-commands/
linux cat tac