每天一个linux命令(10):cat 命令

cat命令的用途是连接文件或标准输入并打印。这个命令常用来显示文件内容,或者将几个文件连接起来显示,或者从标准输入读取内容并显示,它常与重定向符号配合使用。 

cat主要有三大功能:

1.读取短文件显示到标准输出。由于是一次性显示整个文件,如果文件大,前面的部分飞快闪过,无法看到整个文件,这种情况还是要用more、less等页命令。

#cat filename

 

2.从键盘创建一个文件:只能创建新文件,不能编辑已有文件.

kenm1@ken1-server:~/liujin/manual$ cat > no-end-file.txt
  hello
  no-end(Ctrl+d操作,由于没有EOF,其实这个括号及里面的东西实际是没有显示的,像下面那样)
kenm1@ken1-server:~/liujin/manual$ cat no-end-file.txt
  hello
  no-end
kenm1@ken1-server:~/liujin/manual$

 

下面使用结束符:

#cat > myfile.php <<EOF 

>hello

>EOF(回车)  结束输入,存档。

 

3.将几个文件合并为一个文件。

#cat file1 file2 > file

#cat /dev/null > emptyfile.txt  (清空emptyfile.txt文件)

 

NAME
       cat - concatenate files(or standard input) and print on the standard output 

SYNOPSIS
       cat [OPTION]... [FILE]... 

DESCRIPTION
       Concatenate FILE(s), or standard input, to standard output. 

       -n, --number
              number all output lines 

       -b, --number-nonblank
              number nonempty output lines(对非空输出行编号,即空白行不编号) 

       -s, --squeeze-blank
              suppress repeated empty output lines(有连续两行以上的空白行,就代换为一行的空白行) 

 

       -v, --show-nonprinting
              use ^ and M- notation(符号), except for LFD(line feeding?) and TAB 

 

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

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

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

       -e     equivalent to -vE 

       -t     equivalent to -vT 

       -u     (ignored) 

       --help display this help and exit 

       --version
              output version information and exit 

       With no FILE, or when FILE is -, read standard input.

 

EXAMPLES

实例一:把 log2012.log 的文件内容加上行号后输入 log2013.log 这个文件里

命令:

cat -n log2012.log log2013.log 

输出:

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

 

说明:

 

实例二:把 log2012.log 和 log2013.log 的文件内容加上行号(空白行不加)之后将内容附加到 log.log 里。 

命令:

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

输出:

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

 

实例三:把 log2012.log 的文件内容加上行号后输入 log.log 这个文件里 

命令:

输出:

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

 

实例四:使用here doc来生成文件

输出:

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

 

说明:

注意粗体部分,here doc可以进行字符串替换。

 

备注

tac (反向列示)

命令:

tac log.txt

输出:

[root@localhost test]# tac log.txt 

PWD=/opt/soft/test

Linux

World

Hello

说明:

tac 是将 cat 反写过来,所以他的功能就跟 cat 相反, cat 是由第一行到最后一行连续显示在萤幕上,而 tac 则是由最后一行到第一行反向在萤幕上显示出来!

posted @ 2013-10-28 11:52  蓝海孤舟  阅读(158)  评论(0)    收藏  举报