How to use cat commands in linux (display file content)

Source: Internet
Author: User
The cat command is used to connect a file or standard input and

The cat command is used to connect files or standard input and print. This command is often used to display the file content, connect several files, or read and display the content from the standard input. it is often used with the redirection symbol.

1. command format:

Cat [option] [file]...

2. command functions:

Cat has three main functions:

1. display the entire file at a time: cat filename

2. create a file from the keyboard: cat> filename can only create new files, but cannot edit existing files.

3. merge several files into one file: cat file1 file2> file

3. command parameters:

-A, -- show-all is equivalent to-vET.
-B, -- number-nonblank: number of non-empty output rows
-E is equivalent to-vE.
-E, -- show-ends is displayed at the end of each line $
-N, -- number indicates the number of all rows output, starting from 1.
-S, -- squeeze-blank has more than two rows of blank rows in a row.
-T is equivalent to-vT.
-T, -- show-tabs: Display the skip character as ^ I
-U (ignored)
-V, -- show-nonprinting use ^ and M-reference, except LFD and TAB

4. example:

Example 1: add the line number of the log2012.log file and enter the log2013.log file.

Command:

Cat-n log2012.log log2013.log

Output:


Copy codeThe code is as follows:
[Root @ localhost test] # cat log2012.log
2012-01
2012-02

====== [Root @ localhost test] # cat log2013.log
2013-01
2013-02

2013-03
===== [Root @ localhost test] # cat-n log2012.log log2013.log
1 2012-01
2 2012-02
3
4
5 ======
6 2013-01
7 2013-02
8
9
10 2013-03
11 ====== [root @ localhost test] #

 

Note:


Example 2: add the file content of log2012.log and log2013.log to the log. log file by adding the row number (without blank lines.

Command:

Cat-B log2012.log log2013.log log. log

Output:


Copy codeThe code is as follows:
[Root @ localhost test] # cat-B log2012.log log2013.log log. log
1 2012-01
2 2012-02
3 ======
4 2013-01
5 2013-02
6 2013-03
7 ====== [root @ localhost test] #

 

Example 3: add the line number of the log2012.log file and enter log. log in the file.

Command:

Output:


Copy codeThe code is as follows:
[Root @ localhost test] # cat log. log
[Root @ localhost test] # cat-n log2012.log> log. log
[Root @ localhost test] # cat-n log. log
1 2012-01
2 2012-02
3
4
5 ======
[Root @ localhost test] #

 

Example 4: Use here doc to generate a file

Output:


Copy codeThe code is as follows:
[Root @ localhost test] # cat> log.txt < > Hello
> World
> Linux
> PWD = $ (pwd)
> EOF
[Root @ localhost test] # ls-l log.txt
-Rw-r -- 1 root 37 10-28 :07 log.txt
[Root @ localhost test] # cat log.txt
Hello
World
Linux
PWD =/opt/soft/test
[Root @ localhost test] #

 

Note:

Note the bold part. here doc can replace strings.


Note:

Tac (reverse list)

Command:

Tac log.txt

Output:


Copy codeThe code is as follows:
[Root @ localhost test] # tac log.txt
PWD =/opt/soft/test
Linux
World
Hello

 

Tac reversed cat, so its function is opposite to cat. cat is displayed continuously on the screen from the first row to the last row, tac is displayed on the screen from the last line to the first line!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.