您的位置:首页 > 运维架构 > Linux

linux 常用基础命令 cat 详细介绍

2015-11-08 23:45 841 查看
cat 输出文件内容:

命令说明:
cat(Concatenate的缩写),一条linux内置命令,把一个或者多个文件连接在一起,并标准输出或输入。常用来显示文件内容,或者将几个文件连接起来显示,或者从标准输入读取内容并显示。它常与重定向符号配合使用。

命令功能:
a) 一次显示整个文件:catfilename
b) 从键盘创建一个文件:cat> filename 只能创建新文件,不能编辑已有文件
c) 将几个文件合并为一个文件:catfile1 file2 > file
注:

cat file1 file2 > file file1、file2 会覆盖掉file内原有的内容;
cat file1 file2 >> file file1、file2 会追加到file原有内容的后面。

命令格式:

[root@ilinux ~]#cat [选项] [文件]


命令参数:




命令实例:

实例1:显示文件内容
[root@ilinux test]# cat a.txt
2

3
qw
sd
d


实例2:显示文件内容并加上行号
[root@ilinux test]# cat -n a.txt
1   2
2
3
4   3
5   qw
6   sd
7   d


实例3:显示文件内容并加上行号忽略掉空行
[root@ilinux test]# cat -b a.txt
1   2

2   3
3   qw
4   sd
5   d


实例4:显示文件的内容,当遇到有连续两行以上的空白行,就代换为一行的空白行
[root@ilinux test]# cat -s a.txt
2

3
qw
sd
d


拓展:

实例5:把 b.txt 的文件内容加上行号后输入到a.txt 文件里
[root@ilinux test]# cat b.txt
hello linux
[root@ilinux test]# cat a.txt
qwe
asdf
zxc


[root@ilinux test]# cat -n b.txt a.txt
1   hello linux
2   qwe
3   asdf
4   zxc


实例6:把 b.txt 和 c.txt 的文件内容加上行号(空白行不加)后输入到 a.txt 文件里
[root@ilinux test]# cat b.txt
hello linux
[root@ilinux test]# cat c.txt
ln

linux


[root@ilinux test]# cat a.txt
qwe
asdf
zxc


[root@ilinux test]# cat -b b.txt c.txt a.txt
1   hello linux
2   ln

3   linux
4   qwe
5   asdf
6   zxc


实例7:使用 here doc 来生成文件
[root@ilinux test]# cat >a.txt <<EOF
> lnlinux
> hello
> EOF


[root@ilinux test]# ls -l a.txt
-rw-r--r-- 1 root root 14 Nov 7 11:41 a.txt
[root@ilinux test]# cat a.txt
lnlinux
hello


说明:here doc可以进行字符串替换。

反向列示:tac
[root@ilinux test]# tac a.txt
hello
lnlinux


说明:tac 是将 cat 反写过来,所以他的功能就跟 cat 相反, cat 是由第一行到最后一行连续显示在屏幕上,而 tac 则是由最后一行到第一行反向在屏幕上显示出来!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  linux 常用命令 cat