The Cat Command in Linux

Concatenation Explained with Bash Examples

Cat in Linux stands for concatenation (to merge things together) and is one of the most useful and versatile Linux commands. While not exactly as cute and cuddly as a real cat, the Linux cat command can be used to support a number of operations utilizing strings, files, and output.

在这里插入图片描述

The cat command has three primary purposes involving text files:

cat命令具有涉及文本文件的三个主要目的

  • Create
  • Read/Display
  • Update/Modify

We’ll go over each of these in turn to show the commands and options associated with each operation.

依次检查每一个,以显示与每个操作关联的命令和选项

Getting Started

To start out, let’s create a couple of files called foo.txt and spam.txt.

首先,让我们创建几个名为foo.txt和spam.txt的文件

Let’s start by creating foo.txt with the command cat > foo.txt from the Linux command line.

首先从Linux命令行使用cat > foo.txt命令创建foo.txt

Warning: If there is already a file named foo.txt, the cat command using the > operator WILL overwrite it.

警告:如果已经有一个名为foo.txt的文件,则使用>运算符的cat命令将覆盖它

From here the prompt will display a newline that will allow us to input the text we want. For this example we’ll use:

FILE 1
foo
bar
baz

To get back to the command line and create the text file we use CTRL + D.

要返回命令行并创建文本文件,使用CTRL +D

Now let’s create spam.txt with cat > spam.txt and put in the following:

FILE 2
spam
ham
eggs

If we wanted to append or add more text to these files we would use cat >> FILENAME and input the text we want to use.

如果要向这些文件追加或添加更多文本,将使用cat >> FILENAME并输入要使用的文本

Note that the >> operator is used for appending as opposed to the > operator.

请注意,>>运算符用于附加,而不是>运算符

Instead of having to open a text editor, we were able to create a quick and simple text file from the command line, saving us time and effort.

不必打开文本编辑器,可以从命令行创建一个快速简单的文本文件,从而节省了我们的时间和精力

The key takeaway from this section is that we use cat > FILENAME to create or overwrite a file. Additionally, we can use cat >> FILENAME to append to a file that’s already there. Then after typing in the text we want we use CTRL + D to exit the editor, return to the command line, and create the file.

本节的主要内容是,使用cat> FILENAME创建或覆盖文件。另外,我们可以使用cat >> FILENAME附加到已经存在的文件中。然后,在输入文本后,我们希望使用CTRL + D退出编辑器,返回到命令行并创建文件

Reading Rainbow

Now that we’ve created something let’s take a look at what we’ve made.

Notice how we don’t have a > or a >> operator in the following command, only cat and the filename.

The command cat foo.txt will display the following:

FILE 1
foo
bar
baz

So cat foo.txt will let us read the file, but let’s see what else we can do.

Say we wanted to figure out how many lines a file we were working on was. For this the -n option comes in handy.

假设我们想弄清楚我们正在处理的文件有多少行。为此,-n选项非常有用

With the command cat -n foo.txt we can see how long our file is:

1  FILE 1
2  foo
3  bar
4  baz

With -n we can get an idea of how many lines the file we’re working with has. This can come in handy when we’re iterating over a file of a fixed length.

使用-n,我们可以了解正在处理的文件有多少行。当我们遍历固定长度的文件时,这可能会派上用场

ConCATenating files

Ok, so we’ve seen that cat can create and display files, but what about concatenating (combining) them?

好的,所以我们已经看到cat可以创建和显示文件,但是如何将它们串联(合并)呢?

For this example we’ll use files foo.txt and spam.txt. If we want to get fancy we can take a look at the contents of both files at the same time. We’ll use the cat command again, this time using cat foo.txt spam.txt .

在此示例中,我们将使用文件foo.txt和spam.txt。如果我们想花哨的话,我们可以同时查看两个文件的内容。我们将再次使用cat命令,这次使用cat foo.txt spam.txt

FILE 1
foo
bar
baz
FILE 2
spam
ham
eggs

Note that the above only DISPLAYS the two files. At this point we haven’t concatenated them into a new file yet.

请注意,以上仅显示两个文件。此时,我们还没有将它们串联到一个新文件中

To concatenate the files into a new file we want to use cat foo.txt spam.txt > fooSpam.txt which gives us the result into a new file fooSpam.txt.

要将文件串联成一个新文件,我们要使用cat foo.txt spam.txt> fooSpam.txt,这会将结果带入一个新文件fooSpam.txt中

Using cat fooSpam.txt outputs the following to the terminal:

FILE 1
foo
bar
baz
FILE 2
spam
ham
eggs

This command is also useful for when we want to concatenate more than two files into a new file.

当我们要将两个以上的文件连接成一个新文件时,此命令也很有用

The takeaways here are we can view multiple files with cat FILENAME1 FILENAME 2.

这里的要点是,我们可以使用cat FILENAME1 FILENAME 2查看多个文件

Furthermore we can concatenate multiple files into one file with the command cat FILENAME1 FILENAME 2 > FILENAME3.

此外,我们可以使用命令cat FILENAME1 FILENAME 2> FILENAME3将多个文件合并为一个文件

Other Fun Things to do With Cat(s)

Let’s say we’re working with a file and we keep getting errors for some reason before the end of the file – and it looks like it might have more lines than we expected it to.

假设我们正在处理一个文件,并且由于某种原因而在文件结束之前不断出现错误-看起来它的行数可能比我们预期的要多

To investigate the file a bit further and possibly solve our problem we can use the -A switch. The -A option will show us where lines end with a $, it will show us tab characters with a ^I, and it also displays other non-printing characters.

为了进一步研究该文件并尽可能地解决我们的问题,可以使用-A开关。-A选项将向我们显示行以$结尾的位置,它将向我们显示带有^ I的制表符,并且还将显示其它非打印字符

If we were looking at an example of a non-printable text file with cat nonPrintExample.txt we might get out something like this:

如果我们使用cat nonPrintExample.txt查看一个不可打印的文本文件的示例,可能会得到如下信息





       





Which is OK but may not tell us a full story of a character or string that might be causing us issues.

Whereas cat -A nonPrintExample.txt might give us more useful output:

cat -A nonPrintExample.txt可能会为我们提供更多有用的输出:

^I^I$
$
^L$
$
^G^H^H^H^Y^I^N^O^P^@$
^@^@^[g^[f^[d^[g^[6^[5^[4^[6^[=$
$
$
^X$

Here, we get a clearer representation of what might be going on between tabs, line feeds, returns, and other characters.

The takeaway here is that cat -A FILENAME can tell us more in-depth details about the file that we’re working with.

This article should give you a good overview of the cat command, what it can do, and its functionality.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值