cat:concatenate files and print on the standard output合并文件并输出
- 主要用法







实例一:把 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