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 enter 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 with line numbers (no blank lines) and 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]#
实例3:把 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]#
实例4:使用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 则是由最后一行到第一行反向在萤幕上显示出来!