cat 合并文件或查看文件内容

1.命令功能

cat 合并文件或者查看文件内容。

2.语法格式

cat   option    file

参数说明

参数

参数说明

-n

打印文本,并显示每行行号并且空白行也同样包括

-b

与-n用法类似,不同之处,-b不显示空白行的行号,忽略空白行号

-s

当遇到连续2行以上空白行时,重置成一行

-E

在每行结尾处加上$符号

-e

等价于-vE

cat命令分为三类:

  1. 合并多个文件并标准输出
  2. 文件追加到另一个文件中
  3. 创建文件以及写入文件内容的用法 语法:cat > filename <<EOF

3.使用范例

范例1 显示文件内容

[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

范例2 显示行号 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

范例3 空白行不显示行号

[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

范例4 连续多行空白,重置成一行空白

[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

范例5 cat 合并多个文件

[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

范例6  cat 追加(当追加文件不存在时,创建文件)

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

[root@localhost test]# cat 3.txt

123 123

123 123

234 234

234 234

范例7  cat不覆盖追加

[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

范例8  cat 写入文件

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

> HELLO WORLD

> WELCOME TO LINUX

> EOF

[root@localhost test]# cat test.txt

HELLO WORLD

WELCOME TO LINUX

范例 9  cat追加文件,不覆盖

[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

猜你喜欢

转载自www.cnblogs.com/joechu/p/8916032.html