码迷,mamicode.com
首页 > 系统相关 > 详细

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

时间:2015-11-09 01:46:06      阅读:260      评论:0      收藏:0      [点我收藏+]

标签:linux 常用命令 cat

cat 输出文件内容:


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

  2. 命令功能:

    a)  一次显示整个文件:catfilename

    b)  从键盘创建一个文件:cat> filename 只能创建新文件,不能编辑已有文件

    c)  将几个文件合并为一个文件:catfile1 file2 > file

    注: cat file1 file2 > file  file1、file2 会覆盖掉file内原有的内容;

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

  3. 命令格式:

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

  4. 命令参数:

    技术分享

  5.  命令实例:

    实例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 则是由最后一行到第一行反向在萤幕上显示出来!


本文出自 “LnLinux” 博客,请务必保留此出处http://lnlinux.blog.51cto.com/1331535/1710858

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

标签:linux 常用命令 cat

原文地址:http://lnlinux.blog.51cto.com/1331535/1710858

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!