zoukankan      html  css  js  c++  java
  • 每日回顾Shell —cat,tail,head

    Shell中常常会用到cat命令。可是总是不是特别清楚:


    cat命令的用途是连接文件或标准输入并打印。

    这个命令经常使用来显示文件内容。或者将几个文件连接起来显示。或者从标准输入读取内容并显示,它常与重定向符号配合使用。 

    1.命令格式:

    cat [选项] [文件]...

    2.命令功能:

    cat主要有三大功能:

    1.一次显示整个文件:cat filename

    2.从键盘创建一个文件:cat > filename 仅仅能创建新文件,不能编辑已有文件.

    3.将几个文件合并为一个文件:cat file1 file2 > file

    3.命令參数:

    -A, --show-all           等价于 -vET

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

    -e                       等价于 -vE

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

    -n, --number     对输出的全部行编号,由1開始对全部输出的行数编号

    -s, --squeeze-blank  有连续两行以上的空白行,就代换为一行的空白行 

    -t                       与 -vT 等价

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

    -u                       (被忽略)

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


    实例一:把 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 #用到了EOF。表示假设碰到EOF。那么就表示输入结束       并且这里是先cat>log.txt表示新建一个文件

    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]#


    tac (反向列示)

    命令:

    tac log.txt

    输出:

    [root@localhost test]# tac log.txt 

    PWD=/opt/soft/test

    Linux

    World

    Hello

    说明:

    tac 是将 cat 反写过来。所以他的功能就跟 cat 相反, cat 是由第一行到最后一行连续显示在萤幕上,而 tac 则是由最后一行到第一行反向在萤幕上显示出来!


    以下開始介绍tail


    tail 命令指定点開始将文件写到标准输出.使用tail命令的-f选项能够方便的查阅正在改变的日志文件,tail -f filename会把filename里最尾部的内容显示在屏幕上,而且不但刷新,使你看到最新的文件内容. 

    1.命令格式;

    tail[必要參数][选择參数][文件]   

    2.命令功能:

    用于显示指定文件末尾内容,不指定文件时,作为输入信息进行处理。经常使用查看日志文件

    3.命令參数:

    -f 循环读取

    -q 不显示处理信息

    -v 显示具体的处理信息

    -c<数目> 显示的字节数

    -n<行数> 显示行数

    --pid=PID 与-f合用,表示在进程ID,PID死掉之后结束. 

    -q, --quiet, --silent 从不输出给出文件名称的首部 

    -s, --sleep-interval=S 与-f合用,表示在每次重复的间隔休眠S秒 

    4.使用实例:

    显示文件末尾内容

    命令:

    tail -n 5 log2014.log  #(直接表示显示最后5行)

    输出:

    [root@localhost test]# tail -n 5 log2014.log 

    2014-09

    2014-10

    2014-11

    2014-12

    ==============================[root@localhost test]#

    说明:

    显示文件最后5行内容


    循环查看文件内容

    命令:

    tail -f test.log

    输出:

    [root@localhost ~]# ping 192.168.120.204 > test.log &

    [1] 11891[root@localhost ~]# tail -f test.log                  #在不停地ping的过程中,循环显示

    PING 192.168.120.204 (192.168.120.204) 56(84) bytes of data.

    64 bytes from 192.168.120.204: icmp_seq=1 ttl=64 time=0.038 ms

    64 bytes from 192.168.120.204: icmp_seq=2 ttl=64 time=0.036 ms

    64 bytes from 192.168.120.204: icmp_seq=3 ttl=64 time=0.033 ms

    64 bytes from 192.168.120.204: icmp_seq=4 ttl=64 time=0.027 ms

    64 bytes from 192.168.120.204: icmp_seq=5 ttl=64 time=0.032 ms

    64 bytes from 192.168.120.204: icmp_seq=6 ttl=64 time=0.026 ms

    64 bytes from 192.168.120.204: icmp_seq=7 ttl=64 time=0.030 ms

    64 bytes from 192.168.120.204: icmp_seq=8 ttl=64 time=0.029 ms

    64 bytes from 192.168.120.204: icmp_seq=9 ttl=64 time=0.044 ms

    64 bytes from 192.168.120.204: icmp_seq=10 ttl=64 time=0.033 ms

    64 bytes from 192.168.120.204: icmp_seq=11 ttl=64 time=0.027 ms

    [root@localhost ~]#

    说明:

    ping 192.168.120.204 > test.log & //在后台ping远程主机。

    并输出文件到test.log;这样的做法也使用于一个以上的档案监视。用Ctrl+c来终止。

     


    从第5行開始显示文件

    命令:

    tail -n +5 log2014.log

    输出:

    [root@localhost test]# cat log2014.log 

    2014-01

    2014-02

    2014-03

    2014-04

    2014-05

    2014-06

    2014-07

    2014-08

    2014-09

    2014-10

    2014-11

    2014-12

    ==============================

    [root@localhost test]# tail -n +5 log2014.log

    2014-05

    2014-06

    2014-07

    2014-08

    2014-09

    2014-10

    2014-11

    2014-12

    ==============================


    说明: -n 5 表示最后5行的数据。 -n +5表示正数的低5行開始

    都已经说到tail,那么就应该顺带着将head也复习了

    head 与 tail 就像它的名字一样的浅显易懂,它是用来显示开头或结尾某个数量的文字区块。head 用来显示档案的开头至标准输出中,而 tail 想当然尔就是看档案的结尾 

    1.命令格式:

    head [參数]... [文件]...  

    2.命令功能:

    head 用来显示档案的开头至标准输出中默认head命令打印其对应文件的开头10行。 

    3.命令參数:

    -q 隐藏文件名称

    -v 显示文件名称

    -c<字节> 显示字节数

    -n<行数> 显示的行数

    4.使用实例:


       显示文件的前n行

    命令:

    head -n 5 log2014.log

    输出:

    [root@localhost test]# cat log2014.log 

    2014-01

    2014-02

    2014-03

    2014-04

    2014-05

    2014-06

    2014-07

    2014-08

    2014-09

    2014-10

    2014-11

    2014-12

    ==============================

    [root@localhost test]# head -n 5 log2014.log 

    2014-01

    2014-02

    2014-03

    2014-04

    2014-05[root@localhost test]#


    显示文件前n个字节

    命令:

    head -c 20 log2014.log           #n 表示行,c 表示的是字节数

    输出:

    [root@localhost test]# head -c 20 log2014.log

    2014-01

    2014-02

    2014

    [root@localhost test]#


    文件的除了最后n个字节以外的内容 

    命令:

    head -c -32 log2014.log

    输出:

    [root@localhost test]# head -c -32 log2014.log

    2014-01

    2014-02

    2014-03

    2014-04

    2014-05

    2014-06

    2014-07

    2014-08

    2014-09

    2014-10

    2014-11

    2014-12[root@localhost test]#


    输出文件除了最后n行的所有内容

    命令:

    head -n -6 log2014.log

    输出:

    [root@localhost test]# head -n -6 log2014.log

    2014-01

    2014-02

    2014-03

    2014-04

    2014-05

    2014-06

    2014-07[root@localhost test]#


    说明:假设须要取反的话,那么就在-n -c  之后的数字上加上“-”号


    版权声明:本文博客原创文章,博客,未经同意,不得转载。

  • 相关阅读:
    Spring框架开发的三种模式
    IDEA 的Surround With快捷键
    Spring框架IOC和AOP的实现原理与详解
    mitmproxy 安装配置
    adb 使用
    小象代理
    requests 模块查看请求的ip地址
    smtplib 邮件模块
    淘宝直播数据爬取 + 淘宝模拟登陆
    postgresql基础操作
  • 原文地址:https://www.cnblogs.com/lcchuguo/p/4658357.html
Copyright © 2011-2022 走看看