zoukankan      html  css  js  c++  java
  • 每天一个Linux命令(8)cat命令

        cat命令连接文件并打印到标准输出设备上,cat经常用来显示文件的内容,类似于下的type命令。

        注意:当文件较大时,文本在屏幕上迅速闪过(滚屏),用户往往看不清所显示的内容。因此,一般用more等命令分屏显示。为了控制滚屏,可以按Ctrl+S键,停止滚屏;按Ctrl+Q键可以恢复滚屏。按Ctrl+C(中断)键可以终止该命令的执行,并且返回Shell提示符状态。

        (1)用法:

          用法:cat [选项] [文件]...

        (2)功能:

          将[文件]或标准输入组合输出到标准输出。

        (3)选项参数:

          1)-n, --number                                    对输出的所有行编号

          2) -s, --squeeze-blank                           不输出多行空行,有连续两行以上的空白行,就代换为一行的空白行

          3) -E, --show-ends                                在每行结束处显示 $

          4) -b, --number-nonblank                      对非空输出行编号

          5) -A, --show-all                                   等价于 -vET,显示不可打印字符,行尾显示“$”

          6) -T, --show-tabs                                 将跳格字符显示为 ^I

          7) -v, --show-nonprinting                      使用 ^ 和 M- 引用,除了 LFD 和 TAB 之外

          8) --help                                              显示此帮助信息并退出

          9) --version                                          输出版本信息并退出

        (4)实例:

            由于cat命令是查看文档的,所以首先新建文本文档test1.txt,test2.txt,test3.txt并在文档中写入内容:

             方法一:

             (1)首先用touch指令新建三个文档:

    [sunjimeng@localhost Document]$ touch {text1.txt,text2.txt,text3.txt}
    [sunjimeng@localhost Document]$ ll
    总用量 0
    -rw-rw-r--. 1 sunjimeng sunjimeng 0 5月   4 22:18 text1.txt
    -rw-rw-r--. 1 sunjimeng sunjimeng 0 5月   4 22:18 text2.txt
    -rw-rw-r--. 1 sunjimeng sunjimeng 0 5月   4 22:18 text3.txt

             (2)用图形界面,打开文档输入数据:

           

            (3)由于在CentOs里文档有自动备份的功能,因此这里有6个文档。其中带~符号的需要用查看备份的软件来打开: 

              

          (4)查看shell中的文档信息:

    [sunjimeng@localhost Document]$ ll
    总用量 12
    -rw-rw-r--. 1 sunjimeng sunjimeng 61 5月   4 22:23 text1.txt
    -rw-rw-r--. 1 sunjimeng sunjimeng  0 5月   4 22:18 text1.txt~
    -rw-rw-r--. 1 sunjimeng sunjimeng 61 5月   4 22:23 text2.txt
    -rw-rw-r--. 1 sunjimeng sunjimeng  0 5月   4 22:18 text2.txt~
    -rw-rw-r--. 1 sunjimeng sunjimeng 61 5月   4 22:24 text3.txt
    -rw-rw-r--. 1 sunjimeng sunjimeng  0 5月   4 22:18 text3.txt~

            方法二:在shell中直接修改文档的内容:

    [sunjimeng@localhost Document]$ touch text4.txt
    [sunjimeng@localhost Document]$ cat >text4.txt <<EOF
    > test4's first line;
    > test4's second line;
    > test4's third line;
    > EOF
    [sunjimeng@localhost Document]$ ll
    总用量 16
    -rw-rw-r--. 1 sunjimeng sunjimeng 61 5月   4 22:23 text1.txt
    -rw-rw-r--. 1 sunjimeng sunjimeng  0 5月   4 22:18 text1.txt~
    -rw-rw-r--. 1 sunjimeng sunjimeng 61 5月   4 22:23 text2.txt
    -rw-rw-r--. 1 sunjimeng sunjimeng  0 5月   4 22:18 text2.txt~
    -rw-rw-r--. 1 sunjimeng sunjimeng 61 5月   4 22:24 text3.txt
    -rw-rw-r--. 1 sunjimeng sunjimeng  0 5月   4 22:18 text3.txt~
    -rw-rw-r--. 1 sunjimeng sunjimeng 61 5月   4 22:31 text4.txt     //这里并没有创建备份文件,是区别所在
    [sunjimeng@localhost Document]$ 

         1)[sunjimeng@localhost Document]$ cat -n text4.txt                 将包括空行在内的各行按编号输出

    [sunjimeng@localhost Document]$ cat >text4.txt <<EOF                   //先修改text4.txt的内容
    > text4's first line
    > 
    > 
    > text4's second line
    > 
    > text4's third line
    > 
    > 
    > EOF
    [sunjimeng@localhost Document]$ cat -n text4.txt
         1    text4's first line
         2    
         3    
         4    text4's second line
         5    
         6    text4's third line
         7    
         8    

          2)[sunjimeng@localhost Document]$ cat -b text4.txt               将除空行在内的各行按编号输出

    [sunjimeng@localhost Document]$ cat -b text4.txt
         1    text4's first line
    
    
         2    text4's second line
    
         3    text4's third line

          3)[sunjimeng@localhost Document]$ cat text1.txt text2.txt text3.txt        用cat命令直接输出各个文件,可以是一个也可以是多个

    [sunjimeng@localhost Document]$ cat text1.txt text2.txt text3.txt
    test1's first line;
    test1's second line;
    test1's third line;
    test2's first line;
    test2's second line;
    test2's third line;
    test3's first line;
    test3's second line;
    test3's third line;

          4)[sunjimeng@localhost Document]$ cat text1.txt text2.txt > text5.txt             将讲text1.txt和text2.txt输出到text5.txt里,和输出到标准输出一样,也可以有-n,-b等参数

               由于这个特性,cat命令可以将多个压缩包合并成一个,可以用tar命令解压

    # cat test.tar.gz_?? > test.tar.gz #可以用cat命令将被切割的多个压缩包合并成一个
    # tar -xvzf test.tar.gz #再用tar命令解压
    [sunjimeng@localhost Document]$ cat text1.txt text2.txt > text5.txt
    [sunjimeng@localhost Document]$ cat text5.txt
    test1's first line;
    test1's second line;
    test1's third line;
    test2's first line;
    test2's second line;
    test2's third line;
    [sunjimeng@localhost Document]$ 

          5)[sunjimeng@localhost Document]$ tac text5.txt                  倒序输出文件的各行内容

    [sunjimeng@localhost Document]$ tac text5.txt
    test2's third line;
    test2's second line;
    test2's first line;
    test1's third line;
    test1's second line;
    test1's first line;

          6)[sunjimeng@localhost Document]$ cat -s text4.txt                    输出文档中的内容,如果有多个空行则用一个代替

    [sunjimeng@localhost Document]$ cat -s text4.txt                最多连续输出一个空行
    text4's first line
    
    text4's second line
    
    text4's third line
    
    [sunjimeng@localhost Document]$ cat text4.txt                   有多少空行,输出多少空行
    text4's first line
    
    
    text4's second line
    
    text4's third line

          7)[sunjimeng@localhost Document]$ cat >text6.txt                      从键盘录入内容到文件,回车是保存,退出Ctrl+z

    [sunjimeng@localhost Document]$ cat >text6.txt
    I am MenAngel!                                         //除了最后一个回车之后,其余回车是文档中数据的换行并保存
    Practice Order!
    ^Z          //回车后是Ctrl+Z命令退出
    [3]+  已停止               cat > text6.txt
    [sunjimeng@localhost Document]$ cat text6.txt
    I am MenAngel!
    Practice Order!

          8)[sunjimeng@localhost Document]$ cat -E text4.txt                    输出各行文本,并且以$符结尾

    [sunjimeng@localhost Document]$ cat -E text4.txt
    text4's first line$
    $
    $
    text4's second line$
    $
    text4's third line$
    $
    $

          9)[sunjimeng@localhost Document]$ cat >text6.txt <<EOF              用$取表达式的值小小范例:

    [sunjimeng@localhost Document]$ cat >text6.txt <<EOF
    > pwd=$(pwd)
    > EOF
    [sunjimeng@localhost Document]$ cat text6.txt
    pwd=/home/sunjimeng/Document

         10)[sunjimeng@localhost Document]$ cat --help

    [sunjimeng@localhost Document]$ cat --help
    用法:cat [选项]... [文件]...
    将[文件]或标准输入组合输出到标准输出。
    
      -A, --show-all           等于-vET
      -b, --number-nonblank    对非空输出行编号
      -e                       等于-vE
      -E, --show-ends          在每行结束处显示"$"
      -n, --number             对输出的所有行编号
      -s, --squeeze-blank      不输出多行空行
      -t                       与-vT 等价
      -T, --show-tabs          将跳格字符显示为^I
      -u                       (被忽略)
      -v, --show-nonprinting   使用^ 和M- 引用,除了LFD和 TAB 之外
          --help        显示此帮助信息并退出
          --version        显示版本信息并退出
    
    如果没有指定文件,或者文件为"-",则从标准输入读取。
    
    示例:
      cat f - g  先输出f 的内容,然后输出标准输入的内容,最后输出g 的内容。
      cat        将标准输入的内容复制到标准输出。
    
    GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
    请向<http://translationproject.org/team/zh_CN.html> 报告cat 的翻译错误
    要获取完整文档,请运行:info coreutils 'cat invocation'

         11)[sunjimeng@localhost Document]$ cat --version

    [sunjimeng@localhost Document]$ cat --version
    cat (GNU coreutils) 8.22
    Copyright (C) 2013 Free Software Foundation, Inc.
    许可证:GPLv3+:GNU 通用公共许可证第3 版或更新版本<http://gnu.org/licenses/gpl.html>。
    本软件是自由软件:您可以自由修改和重新发布它。
    在法律范围内没有其他保证。
    
    由Torbjörn Granlund 和Richard M. Stallman 编写。
  • 相关阅读:
    Java面试题总结之JDBC 和Hibernate
    Java面试题总结之数据库与SQL语句
    Java面试题总结之OOA/D,UML,和XML
    Java面试题总结之数据结构、算法和计算机基础(刘小牛和丝音的爱情故事1)...
    文件路径的引用问题(配置文件路径vue.config.js)
    vue-cli2引入Bootstrap和jQuery
    ES6常用语法总结
    vue-cli4引入jquery和bootstrap
    vue-router的两种模式(hash和history)及区别
    本地存储localStorage的用法总结
  • 原文地址:https://www.cnblogs.com/MenAngel/p/5461505.html
Copyright © 2011-2022 走看看