Do you really understand the essence of the cat command?

Detailed explanation of cat command under linux

1. Function

Cat has three main functions:

  1. Display the entire file at once.
$ cat filename
  1. Create a file from the keyboard.
$ cat > filename  

You can only create new files, not edit existing files.
3. Combine several files into one file:

 $cat file1 file2 > file

parameter:

parameter meaning
-A, --show-all Equivalent to -vET
-b, --number-nonblank Number non-empty output lines, similar to -n, except that blank lines are not numbered
-e Equivalent to -vE
-E, --show-ends Display $ at the end of each line
-n, --number Number all output lines starting from 1
-s, --squeeze-blank Do not output multiple blank lines. When there are more than two consecutive blank lines, it will be replaced with a single blank line
-t Equivalent to -vT
-T, --show-tabs Display the jump character as ^I
-u (be ignored)
-v, --show-nonprinting Use ^ and M- references, except for LFD and TAB
–help Show this help message and leave

Other examples

Example:
Add the line number to the file content of textfile1 and enter it in the file textfile2

cat -n textfile1 > textfile2

Add the line number to the file contents of textfile1 and textfile2 (blank lines are not added) and then append the contents to textfile3.

cat -b textfile1 textfile2 >> textfile3

Throw the test.txt file into the trash can and assign a null value test.txt

cat /dev/null > /etc/test.txt  

In detail:

Transfer: http://blog.sina.com.cn/s/blog_52f6ead0010127xm.html

cat is a text file viewing and linking tool. To view the content of a file, it is simpler to use cat, which is to directly follow the file name after cat.

such as:

de>[root@localhost ~]# cat /etc/fstabde>
In order to facilitate the novice brothers to flexibly master this tool, let’s talk more about the commonly used parameters;

1.0 cat grammatical structure;

de>cat [option] [file]…de>
option-
A, --show-all is equivalent to -vET
-b, --number-nonblank for non-blank output line number
-e is equivalent to -vE
-E, --show-ends Display $
-n at the end of each line , --number Number all output lines
-s, --squeeze-blank Do not output multiple blank lines
-t is equivalent to -vT
-T, - show-tabs displays jump characters as ^I
-u (ignored)
-v, --show-nonprinting Use ^ and M- for quoting, except for LFD and TAB
-help show this help message and leave

1.1 cat view file content examples;

de>[root@localhost ~]# cat /etc/profile Note: View the content of the profile file in the /etc/ directory;
[root@localhost ~]# cat -b /etc/fstab Note: View the file in the /etc/ directory profile content, and number the non-blank lines, the line number starts from 1;
[root@localhost ~]# cat -n /etc/profile Note: Perform all lines (including blank lines) in the profile in the /etc directory Number output display;
[root@localhost ~]# cat -E /etc/profile Note: View the profile content under /etc/, and append the $ symbol at the end of each line; de>
cat plus parameter -n and nl tool Almost, when the content of the file is output, a line number will be added in front of each line;

de>[root@localhost ~]# cat -n /etc/profile
[root@localhost ~]# nl /etc/profilede>
cat can display the contents of multiple files at the same time. For example, we can display two The contents of a file;

de>[root@localhost ~]# cat /etc/fstab /etc/profilede>
cat For files with extremely large content, they can be transmitted to the more tool through the pipe|, and then viewed page by page;

de>[root@localhost ~]# cat /etc/fstab /etc/profile | morede>

1.2 Examples of cat creation and file connection function;

cat has the function of creating files. After creating a file, it should end with EOF or STOP;

de>[root@localhost ~]# cat> linuxsir.org.txt << EOF Note: Create linuxsir.org.txt file;

I will test cat to create a file, and enter the content for the file; Note: This is the input content for the linuxsir.org.txt file;
North-South-South test; Note: This is the input content for the linuxsir.org.txt file;
EOF Note: Exit Edit status

[root@localhost ~]# cat linuxsir.org.txt Note: Let’s check the content of the linuxsir.org.txt file;

I will test cat to create a file and enter content for the file;
north-south-south test; de>
cat also has the function of appending content to an existing file;

de>[root@localhost ~]# cat linuxsir.txt Note: View the content of the existing file linuxsir.txt;
I am BeiNanNanBei From LinuxSir.Org. Note: The content line
I am writing a document for the cat command

[root@localhost ~]# cat >> linuxsir.txt << EOF Note: We add content to the linuxsir.txt file;

Let me test the function of cat appending content to the document; Note: This is the recovered content
OK?
OK~
North-South submits
EOF Note: Exit with EOF;

[root@localhost ~]# cat linuxsir.txt Note: Check the content of the file to see if the recovery is successful.
I am BeiNanNanBei From LinuxSir.Org. I am
writing documentation for the cat command

Let me test the function of cat appending content to the document;
OK?
OK~
North and South presented de>

cat connects the contents of multiple files and outputs to a new file;

Suppose we have sir01.txt, sir02.tx and sir03.txt, and the content is as follows;

de>[root@localhost ~]# cat sir01.txt
123456
i am testing

[root@localhost ~]# cat sir02.txt
56789
BeiNan Tested

[root@localhost ~]# cat sir03.txt
09876
linuxsir.org testingde>
I want to link the three files sir01.txt, sir02.txt and sir03.txt together through cat (that is, to combine the contents of these three files Are connected together) and output to a new file sir04.txt.

Note: The principle is to connect the contents of the three files, then create a sir04.txt file, and write the contents of several files into sir04.txt at the same time. It is particularly worth mentioning that if you enter an existing sir04.txt file, the content of sir04.txt will be cleared.

de>[root@localhost ~]# cat sir01.txt sir02.txt sir03.txt > sir04.txt

[root@localhost ~]# more sir04.txt
123456
i am testing
56789
BeiNan Tested
09876
linuxsir.org testingde>
cat Append the contents of one or more existing files to an existing file

de>[root@localhost ~]# cat sir00.txt
linuxsir.org forever

[root@localhost ~]# cat sir01.txt sir02.txt sir03.txt >> sir00.txt

[root@localhost ~]# cat sir00.txt
linuxsir.org forever
123456
i am testing
56789
BeiNan Tested
09876
linuxsir.org testing>
Warning: we need to know that> means create, and >> means append. Don't get confused. Making mistakes is not a joke;

Guess you like

Origin blog.csdn.net/daocaokafei/article/details/113079985