linux日志命令一cat和tac

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/uotail/article/details/87414323


linux 命令

cat

cat是 Linux 系统下用来查看文件连续内容用的指令,字面上的含意是concatenate(连续)的缩写

cat主要有三大功能

  1. 一次显示整个文件。$ cat filename
  2. 从键盘创建一个文件。$ cat > filename
    只能创建新文件,或追加内容不能编辑已有内容.
  3. 将几个文件合并为一个文件: $cat file1 file2 > file

语法结构:cat [参数] [文件]

CAT(1)                                                                                                         User Commands                                                                                                        CAT(1)

NAME
       cat - concatenate files and print on the standard output
             连接文件并在标准输出上打印
SYNOPSIS
       cat [OPTION]... [FILE]...

DESCRIPTION
       Concatenate FILE(s) to standard output.

       With no FILE, or when FILE is -, read standard input.

       -A, --show-all
              equivalent to -vET

       -b, --number-nonblank
              number nonempty output lines, overrides -n
              编号非空输出行,覆盖-n

       -e     equivalent to -vE

       -E, --show-ends
              display $ at end of each line
              在每行末尾显示$

       -n, --number
              number all output lines
              给所有输出行编号

       -s, --squeeze-blank
              suppress repeated empty output lines
              抑制重复的空输出行

       -t     equivalent to -vT

       -T, --show-tabs
              display TAB characters as ^I

       -u     (ignored)

       -v, --show-nonprinting
              use ^ and M- notation, except for LFD and TAB

       --help display this help and exit

       --version
              output version information and exit

EXAMPLES
       cat f - g
              Output f's contents, then standard input, then g's contents.

       cat    Copy standard input to standard output.

AUTHOR
       Written by Torbjorn Granlund and Richard M. Stallman.

REPORTING BUGS
       GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
       Report cat translation bugs to <http://translationproject.org/team/>

COPYRIGHT
       Copyright © 2016 Free Software Foundation, Inc.  License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
       This is free software: you are free to change and redistribute it.  There is NO WARRANTY, to the extent permitted by law.

SEE ALSO
       tac(1)

       Full documentation at: <http://www.gnu.org/software/coreutils/cat>
       or available locally via: info '(coreutils) cat invocation'

参数说明:
-n 或 --number:由 1 开始对所有输出的行数编号。
-b 或 --number-nonblank:和 -n 相似,只不过对于空白行不编号。
-s 或 --squeeze-blank:当遇到有连续两行以上的空白行,就代换为一行的空白行。
-E 或 --show-ends : 在每行结束处显示 $。
–help 显示此帮助信息并离开

1 cat 查看文件内容实例

cat /etc/profile     查看/etc/目录下的profile文件内容
cat -b /etc/profile  文件内容编号查看(不包含空白行)
cat -n /etc/profile  文件所有内容编号查看(包含空白行)
cat 加参数-b 和nl工具差不多,文件内容输出的同时,都会在非空白行前面加上行号
cat -E /etc/profile  在每行结束处显示 $

cat 可以同时显示多个文件的内容,比如我们可以在一个cat命令上同时显示两个文件的内容

cat /etc/profile  /etc/ucf.conf 

cat 对于内容极大的文件来说,可以通过管道|传送到more 工具,然后一页一页的查看

cat /etc/profile  /etc/ucf.conf | more

2 cat 的创建、连接、合并文件功能实例

cat 有创建文件的功能,创建文件后,要以EOF或STOP结束

在linux shell脚本中我们经常见到类似于cat << EOF的语句,不熟悉的童鞋可能觉得很奇怪:EOF好像是文件的结束符,用在这里起到什么作用?
EOF是“end of file”,表示文本结束符。

<<EOF
(内容)
EOF

首先必须要说明的是EOF在这里没有特殊的含义,你可以使用FOE或OOO等(当然也不限制在三个字符或大写字符)。

3 几种常见的使用方式及其作用:

   cat file1 file2 > file                                  合并为一个文件
   cat -n linuxfile1 > linuxfile2                    把 linuxfile1 的档案内容加上行号后输入 linuxfile2 这个档案里
   cat -b linuxfile1 linuxfile2 >> linuxfile3   把 linuxfile1 和 linuxfile2 的档案内容加上行号(空白行不加)之后将内容附加到 linuxfile3 里。
   cat /dev/null> word.txt                           把 word.txt文件扔进垃圾箱,赋空值word.txt
  1. cat>filename,创建文件,并把标准输入输出到filename文件中,以ctrl+d作为输入结束:
    注意:输入时是没有’>'的。
  2. cat>filename<<EOF,以EOF作为输入结束,和ctrl+d的作用一样,若存在此文件会覆盖原有内容
  3. cat <filename ,追加内容到文件,之前的内容会覆盖掉
  4. `>> 追加 ; > 覆盖之前内容 ※ 关于“>”、“>>”、“<”、“<<”等的意思,请自行查看bash的介绍。
  5. EOF只是标识,不是固定的
  6. 如果不是在脚本中,我们可以用Ctrl+D输出EOF的标识

4 查看系统信息

cat /proc/cpuinfo 显示CPU info的信息 
cat /proc/interrupts 显示中断 
cat /proc/meminfo 校验内存使用 
cat /proc/swaps 显示哪些swap被使用 
cat /proc/version 显示内核的版本 
cat /proc/net/dev 显示网络适配器及统计 
cat /proc/mounts 显示已加载的文件系统 

tac (反向行显示)

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

TAC(1)                                                                                                         User Commands                                                                                                        TAC(1)

NAME
       tac - concatenate and print files in reverse
             反向连接和打印文件

SYNOPSIS
       tac [OPTION]... [FILE]...

DESCRIPTION
       Write each FILE to standard output, last line first.
       将每个文件写入标准输出,最后一行优先。

       With no FILE, or when FILE is -, read standard input.

       Mandatory arguments to long options are mandatory for short options too.

       -b, --before
              attach the separator before instead of after
              将分隔符附加在前面而不是后面

       -r, --regex
              interpret the separator as a regular expression
              将分隔符解释为正则表达式

       -s, --separator=STRING
              use STRING as the separator instead of newline
              使用字符串作为分隔符,而不是换行符

       --help display this help and exit

       --version
              output version information and exit

AUTHOR
       Written by Jay Lepreau and David MacKenzie.

REPORTING BUGS
       GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
       Report tac translation bugs to <http://translationproject.org/team/>

COPYRIGHT
       Copyright © 2016 Free Software Foundation, Inc.  License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
       This is free software: you are free to change and redistribute it.  There is NO WARRANTY, to the extent permitted by law.

SEE ALSO
       rev(1)

       Full documentation at: <http://www.gnu.org/software/coreutils/tac>
       or available locally via: info '(coreutils) tac invocation'

GNU coreutils 8.25    

猜你喜欢

转载自blog.csdn.net/uotail/article/details/87414323