cat merge files or view file contents

1. Command function

cat Merge files or view file contents.

2. Grammar format

cat   option    file

Parameter Description

parameter

Parameter Description

-n

Print the text, showing each line number and blank lines as well

-b

Similar to the usage of -n, the difference is that -b does not display the line number of blank lines, and ignores blank line numbers

-s

When encountering more than 2 consecutive blank lines, reset to one line

-E

Add a $ sign at the end of each line

-e

Equivalent to -vE

The cat command is divided into three categories:

  1. Merge multiple files and output to standard
  2. Append file to another file
  3. Usage syntax for creating files and writing file contents: cat > filename <<EOF

3. Example of use

Example 1 Display file content

[root@localhost home]# cat passwd_test

root:x:0:0:root:/root:/bin/bash

bin:x:1:1:bin:/bin:/sbin/nologin

 

shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

 

 

 

vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin

saslauth:x:499:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin

 

postfix:x:89:89::/var/spool/postfix:/sbin/nologi

cxf:x:500:500::/home/cxf:/bin/bash

Example 2 Display line number cat -n

[root@localhost home]# cat -n passwd_test

     1  root:x:0:0:root:/root:/bin/bash

     2  bin:x:1:1:bin:/bin:/sbin/nologin

     3

     4  shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

     5

     6

     7

     8  vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin

     9  saslauth:x:499:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin

    10

    11  postfix:x:89:89::/var/spool/postfix:/sbin/nologi

12  cxf:x:500:500::/home/cxf:/bin/bash

Example 3 Blank lines do not display line numbers

[root@localhost home]# cat -b passwd_test

     1  root:x:0:0:root:/root:/bin/bash

     2  bin:x:1:1:bin:/bin:/sbin/nologin

 

     3  shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

 

 

 

     4  vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin

     5  saslauth:x:499:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin

 

     6  postfix:x:89:89::/var/spool/postfix:/sbin/nologi

     7  cxf:x:500:500::/home/cxf:/bin/bash

Example 4 Multiple consecutive blank lines, reset to one blank line

[root@localhost home]# cat -sn passwd_test

     1  root:x:0:0:root:/root:/bin/bash

     2  bin:x:1:1:bin:/bin:/sbin/nologin

     3

     4  shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

     5

     6  vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin

     7  saslauth:x:499:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin

     8

     9  postfix:x:89:89::/var/spool/postfix:/sbin/nologi

    10  cxf:x:500:500::/home/cxf:/bin/bash

 

Example 5 cat merge multiple files

[root@localhost test]# cat 1.txt

123 123

123 123

[root@localhost test]# cat 2.txt

234 234

234 234

[root@localhost test]# cat 1.txt 2.txt

123 123

123 123

234 234

234 234

Example 6 cat append (create file when appending file does not exist)

[root@localhost test]# cat 1.txt 2.txt > 3.txt

[root@localhost test]# cat 3.txt

123 123

123 123

234 234

234 234

Example 7 cat does not overwrite append

[root@localhost test]# cat 1.txt 2.txt >> 3.txt

[root@localhost test]# cat 3.txt

123 123

123 123

234 234

234 234

123 123

123 123

234 234

234 234

Example 8 cat write to file

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

> HELLO WORLD

> WELCOME TO LINUX

> EOF

[root@localhost test]# cat test.txt

HELLO WORLD

WELCOME TO LINUX

Example 9 cat appends files without overwriting

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

> OK OK OK

> EOF

[root@localhost test]# cat test.txt

HELLO WORLD

WELCOME TO LINUX

OK OK OK

 

Guess you like

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