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

[一文一命令]cat命令详解

2013-12-20 14:57 337 查看

Cat

名称: cat cancatenate files and print on the standard output
功能描述: concatenate file or standard input, to standard output
语法: cat [option] [file]
参数:
-A 输出所有的,相当于-vET参数
-b --number-nonblank:输出行号,和N选项一样,不显示空白行。
-n --number 显示所有行的行号 [number all output lines]
-e 相当于-vE参数
-E --show-ends 在每个行末尾显示$符号。
-s --squeeze-blank 当遇见两个空白行时就用一个空白行代替[nevermore than one blank line]
-T --show-tables 将跳字符显示为^I
例:

1)查看/etc/profile文件内容
[root@localhost ~]# cat /etc/profile
# /etc/profile
# System wide environment and startup programs, forlogin setup
# Functions and aliases go in /etc/bashrc
pathmunge ()
内容一下子就显示出来了,所以最好加上分频显示的命令。Cat /etc/profile |more

2)在每行的末尾多显示一个$符号 cat –E test.txt
[root@linux-lwt tmp]# cat -E 1.txt
123$
123$
[root@linux-lwt tmp]# cat 1.txt
123
123
3)同时显示多个文件
[root@localhost ~]# cat test1.txt test.txt
test1.txt
test.txt

4)通过cat创建一个文件,并编辑文档内容。
[root@localhost ~]# cat >> test3 <<EOF
> test3
> test3
> eof #这个不是具体的命令标识,所以一定要一样的都是大写或者都是小写。
> EOF
再次输入以上命令为追加内容到test3文档中。有时候可以用来快捷的修改一些文档。不用vi那样操作那么多步。
5)将两个文档的内容连接,并输入到新文档中。
[root@localhost ~]# cat test1.txt test.txt > test4
[root@localhost ~]# cat test4
1 test.txt
2 test.txt
3 test.txt
4 asf
5 ass
6 f
6)cat 把一个或多个已存在的文档内容,追加到一个已存在的文档。
cat 1.txt 2.txt >> 3.txt
(7)显示行号
[root@linux-lwt tmp]# cat -n 1.txt
1 123
2 123
3 123
(8)创建一个文件
cat > filename 输入完成后按ctrl+d结束输入。
(9)清空文件
> filename
警告:我们要知道>意思是创建,>>是追加。千万不要弄混了。

-----------------------------后续自我小结--------------------------------

cat功能上来将也是很简单啊,但是就有很多灵活的用法。能够合并多个文件,显示文件内容,追歼文件内容,创建文件,清空文件等。
--------------------------------------------------------------------------
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息