Linux common commands: cat command

  The purpose of the cat command is to concatenate files or standard input and print. This command is often used to display the contents of a file, or to concatenate several files for display, or to read the contents from standard input and display it. It is often used in conjunction with the redirection symbol. 

1. Command format:

cat [options] [file]...

2. Command function:

cat has three main functions:

1. Display the entire file at once: cat filename

2. Create a file from the keyboard: cat > filename can only create a new file, not edit an existing file.

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

3. Command parameters:

-A, --show-all is equivalent to -vET

-b, --number-nonblank Number non-blank output lines

-e is equivalent to -vE

-E, --show-ends show $ at the end of each line

-n, --number Number all output lines, starting from 1 to number all output lines

-s, --squeeze-blank If there are more than two consecutive blank lines, replace them with one blank line 

-t is equivalent to -vT

-T, --show-tabs display tab characters as ^I

-u (ignored)

-v, --show-nonprinting use ^ and M- quoting, except LFD and TAB

4. Example of use:

Example 1: Add the file content of log2012.log to the line number and input it into the file log2013.log

Order:

cat -n log2012.log log2013.log 

output:

[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]#

 

illustrate:

Example 2: Add the file contents of log2012.log and log2013.log to the line number (no blank lines), and then append the contents to log.log. 

Order:

cat -b log2012.log log2013.log log.log

output:

[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 file content of log2012.log to the line number and input it into the file log.log 

Order:

output:

[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 files

output:

[root@localhost test]# cat >log.txt <<EOF

> Hello

> World

> Linux

> PWD=$(pwd)

> EOF

[root@localhost test]# ls -l log.txt 

-rw-r--r-- 1 root root 37 10-28 17:07 log.txt

[root@localhost test]# cat log.txt 

Hello

World

Linux

PWD=/opt/soft/test

[root@localhost test]#

illustrate:

  Note the bold part, here doc can do string substitution.

Remark:

tac (reverse listing)

Order:

tac log.txt

output:

[root@localhost test]# tac log.txt 

PWD=/opt/soft/test

Linux

World

Hello

illustrate:

  tac is the reverse of cat, so its function is the opposite of cat. cat is continuously displayed on the screen from the first line to the last line, while tac is displayed on the screen in reverse from the last line to the first line !

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325073314&siteId=291194637