linux的cat命令使用_如何在Linux中使用“ cat”命令合并文本文件
linux的cat命令使用The cat command is very useful in Linux. It has three main functions related to manipulating text files: creating them, displaying them, and combining them.cat命令在Linux中非常有用。 它具有与操纵文本文件相..
linux的cat命令使用
The cat command is very useful in Linux. It has three main functions related to manipulating text files: creating them, displaying them, and combining them.
cat命令在Linux中非常有用。 它具有与操纵文本文件相关的三个主要功能:创建文本,显示文本和组合文本。
We’ve discussed using the cat command (among others) to create and view text files on the command line in Linux. But let’s assume you have three text files: file1.txt, file2.txt, and file3.txt. You want to combine (or concatenate) them into one text file containing information from all three, in that order. You can do this with the cat command as well.
我们讨论了使用cat命令(以及其他方法)在Linux的命令行上创建和查看文本文件 。 但是,假设您有三个文本文件:file1.txt,file2.txt和file3.txt。 您想将它们组合(或连接 )成一个文本文件,其中包含来自这三个文件的信息。 您也可以使用cat命令执行此操作。
Simply open a Terminal and type the following command:
只需打开终端并输入以下命令:
cat file1.txt file2.txt file3.txt
Obviously, replace the file names in the above example with your own.
显然,将上面示例中的文件名替换为您自己的文件名。
The combined contents of the three text files will appear in your terminal.
这三个文本文件的合并内容将出现在终端中。
Typically, though, you’ll probably want to combine those text files into another text file, not just print the results to the screen. Luckily, this is very simple. All you need to do is add an output redirection symbol (>
) after the list of files being concatenated, and then specify the name of the final text file.
但是,通常,您可能希望将这些文本文件合并为另一个文本文件,而不仅仅是将结果打印到屏幕上。 幸运的是,这很简单。 您需要做的就是在要串联的文件列表之后添加一个输出重定向符号 ( >
),然后指定最终文本文件的名称。
cat file1.txt file2.txt file3.txt > file4.txt
NOTE: The file listed after the output redirection symbol will be overwritten, if it already exists. So, be careful when specifying the name of the combined text file. We’ll show you later in this article how to append files to the end of an existing file.
注意:如果输出重定向符号之后列出的文件已经存在,则它将被覆盖。 因此,在指定组合文本文件的名称时要小心。 我们将在本文后面向您展示如何将文件追加到现有文件的末尾。
If you open file4.txt (either with the cat command or with the text editor of your choice), you should find that it contains the text of the first three text files.
如果打开file4.txt(使用cat命令或所选的文本编辑器),则应发现它包含前三个文本文件的文本。
If you’re combining lists of items from multiple files and you want them alphabetized in the combined file, you can sort the combined items in the resulting file. To do this, enter the basic cat
command we previously showed you followed by the pipe command (|) and the sort
command. Then, type the output redirection symbol (>
) followed by the name of the file into which you want to copy the combined text. All the lines of text in the result file will be sorted alphabetically.
如果要合并多个文件中的项目列表,并希望它们按字母顺序显示在合并文件中,则可以对结果文件中的合并项目进行排序。 为此,请输入先前显示的基本cat
命令,然后是管道命令(|)和sort
命令。 然后,键入输出重定向符号( >
),然后键入要将组合文本复制到其中的文件的名称。 结果文件中的所有文本行都将按字母顺序排序。
cat file1.txt file2.txt file3.txt | sort > file4.txt
As we mentioned earlier, there is also a way append files to the end of an existing file. Type the cat
command followed by the file or files you want to add to the end of an existing file. Then, type two output redirection symbols (>>
) followed by the name of the existing file you want to add to.
如前所述,还有一种将文件追加到现有文件末尾的方法。 键入cat
命令,然后键入要添加到现有文件末尾的文件。 然后,键入两个输出重定向符号( >>
),然后键入要添加到的现有文件的名称。
cat file5.txt >> file4.txt
If you want to add a bit of new text to an existing text file, you use the cat
command to do it directly from the command line (instead of opening it in a text editor). Type the cat
command followed by the double output redirection symbol (>>
) and the name of the file you want to add text to.
如果要向现有文本文件中添加一些新文本,请使用cat
命令直接从命令行进行操作(而不是在文本编辑器中打开它)。 键入cat
命令,后跟双输出重定向符号( >>
)和要将文本添加到的文件的名称。
cat >> file4.txt
A cursor will appear on the next line below the prompt. Start typing the text you want to add to the file. When you’re done, press Enter after the last line and then press Ctrl+D to copy that text to the end of the file and quit cat.
光标将出现在提示下方的下一行。 开始输入要添加到文件中的文本。 完成后,在最后一行之后按Enter,然后按Ctrl + D将文本复制到文件末尾并退出cat。
If you end up with a very long file once you combine your text files, you can use the pipe symbol with the less command when viewing the file in the Terminal window. For example, cat file4.txt | less
. We discuss using the less command in this article.
如果合并文本文件后最终得到的文件很长,则在“终端”窗口中查看文件时,可以将管道符号与less命令一起使用。 例如, cat file4.txt | less
cat file4.txt | less
。 我们在本文中讨论使用less命令。
翻译自: https://www.howtogeek.com/278599/how-to-combine-text-files-using-the-cat-command-in-linux/
linux的cat命令使用
更多推荐
所有评论(0)