Linux文本处理工具

                Linux文本处理工具

                                           作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

 

 

一.文本文件查看相关命令

1>.查看cat命令的帮助信息

[root@node101.yinzhengjie.org.cn ~]# cat --help
Usage: cat [OPTION]... [FILE]...
Concatenate FILE(s), or standard input, to standard output.

  -A, --show-all           equivalent to -vET
  -b, --number-nonblank    number nonempty output lines, overrides -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

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

Examples:
  cat f - g  Output f's contents, then standard input, then g's contents.
  cat        Copy standard input to standard output.

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
For complete documentation, run: info coreutils 'cat invocation'
[root@node101.yinzhengjie.org.cn ~]# 

2>.cat命令常用参数

[root@node101.yinzhengjie.org.cn ~]# cat f1.txt 
1

2

3
4

5
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat -n f1.txt       #对文件原每行内容加行号
     1  1
     2
     3  2
     4
     5  3
     6  4
     7
     8  5
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat -n f1.txt          #对文件原每行内容加行号
[root@node101.yinzhengjie.org.cn ~]# cat f1.txt 
1

2

3
4

5
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat -n f1.txt  
     1  1
     2
     3  2
     4
     5  3
     6  4
     7
     8  5
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat -b f1.txt      #和"-n"参数想必,"-b"对空白字符就不加行号,这是两者对区别。
     1  1

     2  2

     3  3
     4  4

     5  5
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat -b f1.txt           #和"-n"参数想必,"-b"对空白字符就不加行号,这是两者对区别。即非空行编号
[root@node101.yinzhengjie.org.cn ~]# cat -n f1.txt 
     1  1
     2
     3  2
     4
     5
     6
     7  3
     8  4
     9
    10  5
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat -sn f1.txt         #通过对比可以发现,"-s"具有将相邻空行压缩成一行输出的功能。
     1  1
     2
     3  2
     4
     5  3
     6  4
     7
     8  5
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat -sn f1.txt         #通过对比可以发现,"-s"具有将相邻空行压缩成一行输出的功能。
[root@node101.yinzhengjie.org.cn ~]# cat f1.txt 
1

2       a       b



3
4

5
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat -A f1.txt     #显示所有控制符。
1$          #注意,"$"表示换行,类似于Linux的换行符"\n"。
$
2^Ia^Ib$       #注意,"^I"表示tab键  
$
$
$
3$
4$
$
5$
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# vi f1.txt     #在命令模式中执行":set ff=dos"就可以将Linux操作系统的格式指定为dos格式。 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat -A f1.txt 
1^M$        #注意,"^M"表示回车,"$"表示换行,合起来类似于windows的换行符"\r\n"^M$
2^Ia^Ib^M$
^M$
^M$
^M$
3^M$
4^M$
^M$
5^M$
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat -A f1.txt          #显示所有控制符。
[root@node101.yinzhengjie.org.cn ~]# cat f1.txt 
1

2       a       b



3
4

5
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat -E f1.txt            #显示行结束符"$"
$
$
$       a       b
$
$
$
$
$
$
$
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat -E f1.txt          #显示行结束符"$"

3>.使用nl命令也可以查看行号

[root@node101.yinzhengjie.org.cn ~]# cat f1.txt 
1

2       a       b



3
4

5
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# nl f1.txt         #为nl命令添加行号,效果和"cat -n f1.txt"功能等效。
     1  1
     2
     3  2       a       b
     4
     5
     6
     7  3
     8  4
     9
    10  5
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat -n f1.txt 
1 1
2
3 2 a b
4
5
6
7 3
8 4
9
10 5
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# nl f1.txt         #为nl命令添加行号,效果和"cat -n f1.txt"功能等效。

4>.使用tac命令倒着查看文件内容

[root@node101.yinzhengjie.org.cn ~]# cat f1.txt 
1

2       a       b



3
4

5
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# tac f1.txt         #和"cat"命令都是查看文件内容,只不过它和cat查看内容的起始点不同,"tac"从最后一行到第一行去查看文件内容。但每行内容不变!
5

4
3



2       a       b

1
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# tac f1.txt         #和"cat"命令都是查看文件内容,只不过它和cat查看内容的起始点不同,"tac"从最后一行到第一行去查看文件内容。但每行内容不变!

5>.使用rev命令将每行内容倒着看

[root@node101.yinzhengjie.org.cn ~]# echo abcdefg | rev     #rev命令可以将内容标准输入但数据倒着输出
gfedcba
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat f1.txt 
1

2       a       b



3
4

5
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# rev f1.txt         #很明显,"rev"命令可以将每行内容倒着查看。
1

b       a       2



3
4

5
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# rev f1.txt         #很明显,"rev"命令可以将每行内容倒着查看。

  

二.查看非文本文件内容

1>.hexdump命令 

[root@node101.yinzhengjie.org.cn ~]# hexdump --help      #查看该命令的帮助信息
hexdump: invalid option -- '-'

Usage:
 hexdump [options] file...

Options:
 -b              one-byte octal display
 -c              one-byte character display
 -C              canonical hex+ASCII display
 -d              two-byte decimal display
 -o              two-byte octal display
 -x              two-byte hexadecimal display
 -e format       format string to be used for displaying data
 -f format_file  file that contains format strings
 -n length       interpret only length bytes of input
 -s offset       skip offset bytes from the beginning
 -v              display without squeezing similar lines
 -V              output version information and exit

[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# hexdump --help             #查看该命令的帮助信息
[root@node101.yinzhengjie.org.cn ~]# hexdump -C -n 512 /dev/sda      #我们知道使用cat命令查看块设备"/dev/sda"会出现乱码字符,我们这里的"=C"表示以标准十六进制和ASCII显示该设备文件内容,-n表示查看该文件的长度。
00000000  eb 63 90 10 8e d0 bc 00  b0 b8 00 00 8e d8 8e c0  |.c..............|
00000010  fb be 00 7c bf 00 06 b9  00 02 f3 a4 ea 21 06 00  |...|.........!..|
00000020  00 be be 07 38 04 75 0b  83 c6 10 81 fe fe 07 75  |....8.u........u|
00000030  f3 eb 16 b4 02 b0 01 bb  00 7c b2 80 8a 74 01 8b  |.........|...t..|
00000040  4c 02 cd 13 ea 00 7c 00  00 eb fe 00 00 00 00 00  |L.....|.........|
00000050  00 00 00 00 00 00 00 00  00 00 00 80 01 00 00 00  |................|
00000060  00 00 00 00 ff fa 90 90  f6 c2 80 74 05 f6 c2 70  |...........t...p|
00000070  74 02 b2 80 ea 79 7c 00  00 31 c0 8e d8 8e d0 bc  |t....y|..1......|
00000080  00 20 fb a0 64 7c 3c ff  74 02 88 c2 52 be 05 7c  |. ..d|<.t...R..||
00000090  b4 41 bb aa 55 cd 13 5a  52 72 3d 81 fb 55 aa 75  |.A..U..ZRr=..U.u|
000000a0  37 83 e1 01 74 32 31 c0  89 44 04 40 88 44 ff 89  |7...t21..D.@.D..|
000000b0  44 02 c7 04 10 00 66 8b  1e 5c 7c 66 89 5c 08 66  |D.....f..\|f.\.f|
000000c0  8b 1e 60 7c 66 89 5c 0c  c7 44 06 00 70 b4 42 cd  |..`|f.\..D..p.B.|
000000d0  13 72 05 bb 00 70 eb 76  b4 08 cd 13 73 0d 5a 84  |.r...p.v....s.Z.|
000000e0  d2 0f 83 de 00 be 85 7d  e9 82 00 66 0f b6 c6 88  |.......}...f....|
000000f0  64 ff 40 66 89 44 04 0f  b6 d1 c1 e2 02 88 e8 88  |d.@f.D..........|
00000100  f4 40 89 44 08 0f b6 c2  c0 e8 02 66 89 04 66 a1  |.@.D.......f..f.|
00000110  60 7c 66 09 c0 75 4e 66  a1 5c 7c 66 31 d2 66 f7  |`|f..uNf.\|f1.f.|
00000120  34 88 d1 31 d2 66 f7 74  04 3b 44 08 7d 37 fe c1  |4..1.f.t.;D.}7..|
00000130  88 c5 30 c0 c1 e8 02 08  c1 88 d0 5a 88 c6 bb 00  |..0........Z....|
00000140  70 8e c3 31 db b8 01 02  cd 13 72 1e 8c c3 60 1e  |p..1......r...`.|
00000150  b9 00 01 8e db 31 f6 bf  00 80 8e c6 fc f3 a5 1f  |.....1..........|
00000160  61 ff 26 5a 7c be 80 7d  eb 03 be 8f 7d e8 34 00  |a.&Z|..}....}.4.|
00000170  be 94 7d e8 2e 00 cd 18  eb fe 47 52 55 42 20 00  |..}.......GRUB .|
00000180  47 65 6f 6d 00 48 61 72  64 20 44 69 73 6b 00 52  |Geom.Hard Disk.R|
00000190  65 61 64 00 20 45 72 72  6f 72 0d 0a 00 bb 01 00  |ead. Error......|
000001a0  b4 0e cd 10 ac 3c 00 75  f4 c3 00 00 00 00 00 00  |.....<.u........|
000001b0  00 00 00 00 00 00 00 00  7c 37 0e 00 00 00 80 20  |........|7..... |
000001c0  21 00 83 dd 1e 3f 00 08  00 00 00 a0 0f 00 00 dd  |!....?..........|
000001d0  1f 3f 8e fe ff ff 00 a8  0f 00 00 58 f0 07 00 00  |.?.........X....|
000001e0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
000001f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 55 aa  |..............U.|
00000200
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# hexdump -C -n 512 /dev/sda      #我们知道使用cat命令查看块设备"/dev/sda"会出现乱码字符,我们这里的"=C"表示以标准十六进制和ASCII显示该设备文件内容,-n表示查看该文件的长度。

2>.od命令(用于将文件以八进制或者其他进制显示)

[root@node101.yinzhengjie.org.cn ~]# od --help                #查看该命令的帮助信息
Usage: od [OPTION]... [FILE]...
  or:  od [-abcdfilosx]... [FILE] [[+]OFFSET[.][b]]
  or:  od --traditional [OPTION]... [FILE] [[+]OFFSET[.][b] [+][LABEL][.][b]]

Write an unambiguous representation, octal bytes by default,
of FILE to standard output.  With more than one FILE argument,
concatenate them in the listed order to form the input.
With no FILE, or when FILE is -, read standard input.

If first and second call formats both apply, the second format is assumed
if the last operand begins with + or (if there are 2 operands) a digit.
An OFFSET operand means -j OFFSET.  LABEL is the pseudo-address
at first byte printed, incremented when dump is progressing.
For OFFSET and LABEL, a 0x or 0X prefix indicates hexadecimal;
suffixes may be . for octal and b for multiply by 512.

Mandatory arguments to long options are mandatory for short options too.
  -A, --address-radix=RADIX   output format for file offsets; RADIX is one
                                of [doxn], for Decimal, Octal, Hex or None
  -j, --skip-bytes=BYTES      skip BYTES input bytes first
  -N, --read-bytes=BYTES      limit dump to BYTES input bytes
  -S BYTES, --strings[=BYTES]  output strings of at least BYTES graphic chars;
                                3 is implied when BYTES is not specified
  -t, --format=TYPE           select output format or formats
  -v, --output-duplicates     do not use * to mark line suppression
  -w[BYTES], --width[=BYTES]  output BYTES bytes per output line;
                                32 is implied when BYTES is not specified
      --traditional           accept arguments in third form above
      --help     display this help and exit
      --version  output version information and exit


Traditional format specifications may be intermixed; they accumulate:
  -a   same as -t a,  select named characters, ignoring high-order bit
  -b   same as -t o1, select octal bytes
  -c   same as -t c,  select printable characters or backslash escapes
  -d   same as -t u2, select unsigned decimal 2-byte units
  -f   same as -t fF, select floats
  -i   same as -t dI, select decimal ints
  -l   same as -t dL, select decimal longs
  -o   same as -t o2, select octal 2-byte units
  -s   same as -t d2, select decimal 2-byte units
  -x   same as -t x2, select hexadecimal 2-byte units


TYPE is made up of one or more of these specifications:
  a          named character, ignoring high-order bit
  c          printable character or backslash escape
  d[SIZE]    signed decimal, SIZE bytes per integer
  f[SIZE]    floating point, SIZE bytes per integer
  o[SIZE]    octal, SIZE bytes per integer
  u[SIZE]    unsigned decimal, SIZE bytes per integer
  x[SIZE]    hexadecimal, SIZE bytes per integer

SIZE is a number.  For TYPE in [doux], SIZE may also be C for
sizeof(char), S for sizeof(short), I for sizeof(int) or L for
sizeof(long).  If TYPE is f, SIZE may also be F for sizeof(float), D
for sizeof(double) or L for sizeof(long double).

Adding a z suffix to any type displays printable characters at the end of
each output line.


BYTES is hex with 0x or 0X prefix, and may have a multiplier suffix:
  b    512
  KB   1000
  K    1024
  MB   1000*1000
  M    1024*1024
and so on for G, T, P, E, Z, Y.

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
For complete documentation, run: info coreutils 'od invocation'
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# od --help                #查看该命令的帮助信息 
[root@node101.yinzhengjie.org.cn ~]# echo {a..z} 
a b c d e f g h i j k l m n o p q r s t u v w x y z
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# echo {a..z} | tr -d ' ' 
abcdefghijklmnopqrstuvwxyz
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# echo {a..z} | tr -d ' ' | od -A x -t x1z
000000 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70  >abcdefghijklmnop<
000010 71 72 73 74 75 76 77 78 79 7a 0a                 >qrstuvwxyz.<
00001b
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# echo {a..z} | tr -d ' ' | od -A x -t x1z

3>.xxd命令

[root@node101.yinzhengjie.org.cn ~]# yum whatprovides xxd         #使用yum命令检查xxd模块由那个模块提供
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.tuna.tsinghua.edu.cn
 * extras: mirrors.tuna.tsinghua.edu.cn
 * updates: mirrors.tuna.tsinghua.edu.cn
base/7/x86_64/filelists_db                                                                                                                            | 7.3 MB  00:00:00     
extras/7/x86_64/filelists_db                                                                                                                          | 207 kB  00:00:00     
updates/7/x86_64/filelists_db                                                                                                                         | 1.4 MB  00:00:00     
2:vim-common-7.4.629-6.el7.x86_64 : The common files needed by any version of the VIM editor
Repo        : base
Matched from:
Filename    : /usr/bin/xxd



[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# yum whatprovides xxd                    #使用yum命令检查xxd模块由那个模块提供
[root@node101.yinzhengjie.org.cn ~]# yum -y install vim-common-7.4.629-6.el7.x86_64    #根据上条命令查看到xxd依赖于该安装包,安装后就可以使用xxd命令啦
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.tuna.tsinghua.edu.cn
 * extras: mirrors.tuna.tsinghua.edu.cn
 * updates: mirrors.tuna.tsinghua.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package vim-common.x86_64 2:7.4.629-6.el7 will be installed
--> Processing Dependency: vim-filesystem for package: 2:vim-common-7.4.629-6.el7.x86_64
--> Running transaction check
---> Package vim-filesystem.x86_64 2:7.4.629-6.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=============================================================================================================================================================================
 Package                                      Arch                                 Version                                          Repository                          Size
=============================================================================================================================================================================
Installing:
 vim-common                                   x86_64                               2:7.4.629-6.el7                                  base                               5.9 M
Installing for dependencies:
 vim-filesystem                               x86_64                               2:7.4.629-6.el7                                  base                                11 k

Transaction Summary
=============================================================================================================================================================================
Install  1 Package (+1 Dependent package)

Total download size: 5.9 M
Installed size: 21 M
Downloading packages:
(1/2): vim-filesystem-7.4.629-6.el7.x86_64.rpm                                                                                                        |  11 kB  00:00:00     
(2/2): vim-common-7.4.629-6.el7.x86_64.rpm                                                                                                            | 5.9 MB  00:00:00     
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                        8.3 MB/s | 5.9 MB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : 2:vim-filesystem-7.4.629-6.el7.x86_64                                                                                                                     1/2 
  Installing : 2:vim-common-7.4.629-6.el7.x86_64                                                                                                                         2/2 
  Verifying  : 2:vim-common-7.4.629-6.el7.x86_64                                                                                                                         1/2 
  Verifying  : 2:vim-filesystem-7.4.629-6.el7.x86_64                                                                                                                     2/2 

Installed:
  vim-common.x86_64 2:7.4.629-6.el7                                                                                                                                          

Dependency Installed:
  vim-filesystem.x86_64 2:7.4.629-6.el7                                                                                                                                      

Complete!
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# yum -y install vim-common-7.4.629-6.el7.x86_64    #根据上条命令查看到xxd依赖于该安装包,安装后就可以使用xxd命令啦
[root@node101.yinzhengjie.org.cn ~]# xxd --help                  #查看该命令的帮助信息
Usage:
       xxd [options] [infile [outfile]]
    or
       xxd -r [-s [-]offset] [-c cols] [-ps] [infile [outfile]]
Options:
    -a          toggle autoskip: A single '*' replaces nul-lines. Default off.
    -b          binary digit dump (incompatible with -ps,-i,-r). Default hex.
    -c cols     format <cols> octets per line. Default 16 (-i: 12, -ps: 30).
    -E          show characters in EBCDIC. Default ASCII.
    -g          number of octets per group in normal output. Default 2.
    -h          print this summary.
    -i          output in C include file style.
    -l len      stop after <len> octets.
    -ps         output in postscript plain hexdump style.
    -r          reverse operation: convert (or patch) hexdump into binary.
    -r -s off   revert with <off> added to file positions found in hexdump.
    -s [+][-]seek  start at <seek> bytes abs. (or +: rel.) infile offset.
    -u          use upper case hex letters.
    -v          show version: "xxd V1.10 27oct98 by Juergen Weigert".
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# xxd --help                  #查看该命令的帮助信息
[root@node101.yinzhengjie.org.cn ~]# echo {a..z} 
a b c d e f g h i j k l m n o p q r s t u v w x y z
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# echo {a..z} | tr -d ' '           #将空格删除
abcdefghijklmnopqrstuvwxyz
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# echo {a..z} | tr -d ' ' | xxd       #默认以十六进制显示相应内容
0000000: 6162 6364 6566 6768 696a 6b6c 6d6e 6f70  abcdefghijklmnop
0000010: 7172 7374 7576 7778 797a 0a              qrstuvwxyz.
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# echo {a..z} | tr -d ' ' | xxd       #默认以十六进制显示相应内容

 

三.分页查看文本文件内容

1>.more命令用于分页查看文件

[root@node101.yinzhengjie.org.cn ~]# more         #默认显示more命令的帮助信息
Usage: more [options] file...

Options:
  -d        display help instead of ring bell
  -f        count logical, rather than screen lines
  -l        suppress pause after form feed
  -p        do not scroll, clean screen and display text
  -c        do not scroll, display text and clean line ends
  -u        suppress underlining
  -s        squeeze multiple blank lines into one
  -NUM      specify the number of lines per screenful
  +NUM      display file beginning from line number NUM
  +/STRING  display file beginning from search string match
  -V        output version information and exit
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# more         #默认显示more命令的帮助信息
[root@node101.yinzhengjie.org.cn ~]# more /var/log/messages    #查看系统日志文件内容,由于内容过多,一屏幕无法全部显示输出,因此会分页显示且最后一行会有一个进度条
Sep  8 03:41:01 node101 rsyslogd: [origin software="rsyslogd" swVersion="8.24.0-34.el7" x-pid="4104" x-info="http://www.rsyslog.com"] rsyslogd was HUPed
Sep  8 04:01:01 node101 systemd: Started Session 31 of user root.
Sep  8 05:01:01 node101 systemd: Started Session 32 of user root.
Sep  8 06:01:01 node101 systemd: Started Session 33 of user root.
Sep  8 07:01:01 node101 systemd: Started Session 34 of user root.
Sep  8 08:01:01 node101 systemd: Started Session 35 of user root.
Sep  8 08:15:17 node101 chronyd[3566]: Source 193.228.143.12 replaced with 124.108.20.1
Sep  8 09:01:01 node101 systemd: Started Session 36 of user root.
Sep  8 10:01:01 node101 systemd: Started Session 37 of user root.
Sep  8 11:01:01 node101 systemd: Started Session 38 of user root.
Sep  8 12:01:01 node101 systemd: Started Session 39 of user root.
Sep  8 13:00:23 node101 chronyd[3566]: Can't synchronise: no selectable sources
Sep  8 13:01:01 node101 systemd: Started Session 40 of user root.
Sep  8 14:01:01 node101 systemd: Started Session 41 of user root.
Sep  8 15:01:01 node101 systemd: Started Session 42 of user root.
Sep  8 16:01:01 node101 systemd: Started Session 43 of user root.
Sep  8 17:01:02 node101 systemd: Started Session 44 of user root.
Sep  8 18:01:01 node101 systemd: Started Session 45 of user root.
Sep  8 18:10:15 node101 chronyd[3566]: Selected source 45.43.30.59
Sep  8 18:36:16 node101 chronyd[3566]: Source 124.108.20.1 replaced with 139.199.214.202
Sep  8 19:01:01 node101 systemd: Started Session 46 of user root.
Sep  8 20:01:01 node101 systemd: Started Session 47 of user root.
Sep  8 20:37:06 node101 chronyd[3566]: Selected source 139.199.214.202
Sep  8 20:58:19 node101 systemd-logind: Removed session 1.
Sep  8 20:58:19 node101 systemd: Removed slice User Slice of root.
Sep  8 21:01:01 node101 systemd: Created slice User Slice of root.
Sep  8 21:01:01 node101 systemd: Started Session 48 of user root.
Sep  8 21:01:01 node101 systemd: Removed slice User Slice of root.
Sep  8 22:01:01 node101 systemd: Created slice User Slice of root.
Sep  8 22:01:01 node101 systemd: Started Session 49 of user root.
Sep  8 22:01:01 node101 systemd: Removed slice User Slice of root.
Sep  8 23:01:01 node101 systemd: Created slice User Slice of root.
Sep  8 23:01:01 node101 systemd: Started Session 50 of user root.
Sep  8 23:01:01 node101 systemd: Removed slice User Slice of root.
Sep  8 23:04:18 node101 systemd: Starting Cleanup of Temporary Directories...
Sep  8 23:04:18 node101 systemd: Started Cleanup of Temporary Directories.
Sep  8 23:51:00 node101 systemd: Created slice User Slice of root.
Sep  8 23:51:00 node101 systemd-logind: New session 51 of user root.
Sep  8 23:51:00 node101 systemd: Started Session 51 of user root.
Sep  9 00:01:01 node101 systemd: Started Session 52 of user root.
Sep  9 01:01:02 node101 systemd: Started Session 53 of user root.
Sep  9 01:06:52 node101 NetworkManager[3613]: <info>  [1568005612.4339] device (eth0): carrier: link connected
Sep  9 02:00:03 node101 systemd-logind: Removed session 51.
Sep  9 02:00:03 node101 systemd: Removed slice User Slice of root.
--More--(2%)
[root@node101.yinzhengjie.org.cn ~]# more /var/log/messages    #查看系统日志文件内容,由于内容过多,一屏幕无法全部显示输出,因此会分页显示且最后一行会有一个进度条
[root@node101.yinzhengjie.org.cn ~]# more -d /var/log/messages   #注意最后一行,除了显示当前页在占该文件的百分比外,还会有一个退出提示
Sep  8 03:41:01 node101 rsyslogd: [origin software="rsyslogd" swVersion="8.24.0-34.el7" x-pid="4104" x-info="http://www.rsyslog.com"] rsyslogd was HUPed
Sep  8 04:01:01 node101 systemd: Started Session 31 of user root.
Sep  8 05:01:01 node101 systemd: Started Session 32 of user root.
Sep  8 06:01:01 node101 systemd: Started Session 33 of user root.
Sep  8 07:01:01 node101 systemd: Started Session 34 of user root.
Sep  8 08:01:01 node101 systemd: Started Session 35 of user root.
Sep  8 08:15:17 node101 chronyd[3566]: Source 193.228.143.12 replaced with 124.108.20.1
Sep  8 09:01:01 node101 systemd: Started Session 36 of user root.
Sep  8 10:01:01 node101 systemd: Started Session 37 of user root.
Sep  8 11:01:01 node101 systemd: Started Session 38 of user root.
Sep  8 12:01:01 node101 systemd: Started Session 39 of user root.
Sep  8 13:00:23 node101 chronyd[3566]: Can't synchronise: no selectable sources
Sep  8 13:01:01 node101 systemd: Started Session 40 of user root.
Sep  8 14:01:01 node101 systemd: Started Session 41 of user root.
Sep  8 15:01:01 node101 systemd: Started Session 42 of user root.
Sep  8 16:01:01 node101 systemd: Started Session 43 of user root.
Sep  8 17:01:02 node101 systemd: Started Session 44 of user root.
Sep  8 18:01:01 node101 systemd: Started Session 45 of user root.
Sep  8 18:10:15 node101 chronyd[3566]: Selected source 45.43.30.59
Sep  8 18:36:16 node101 chronyd[3566]: Source 124.108.20.1 replaced with 139.199.214.202
Sep  8 19:01:01 node101 systemd: Started Session 46 of user root.
Sep  8 20:01:01 node101 systemd: Started Session 47 of user root.
Sep  8 20:37:06 node101 chronyd[3566]: Selected source 139.199.214.202
Sep  8 20:58:19 node101 systemd-logind: Removed session 1.
Sep  8 20:58:19 node101 systemd: Removed slice User Slice of root.
Sep  8 21:01:01 node101 systemd: Created slice User Slice of root.
Sep  8 21:01:01 node101 systemd: Started Session 48 of user root.
Sep  8 21:01:01 node101 systemd: Removed slice User Slice of root.
Sep  8 22:01:01 node101 systemd: Created slice User Slice of root.
Sep  8 22:01:01 node101 systemd: Started Session 49 of user root.
Sep  8 22:01:01 node101 systemd: Removed slice User Slice of root.
Sep  8 23:01:01 node101 systemd: Created slice User Slice of root.
Sep  8 23:01:01 node101 systemd: Started Session 50 of user root.
Sep  8 23:01:01 node101 systemd: Removed slice User Slice of root.
Sep  8 23:04:18 node101 systemd: Starting Cleanup of Temporary Directories...
Sep  8 23:04:18 node101 systemd: Started Cleanup of Temporary Directories.
Sep  8 23:51:00 node101 systemd: Created slice User Slice of root.
Sep  8 23:51:00 node101 systemd-logind: New session 51 of user root.
Sep  8 23:51:00 node101 systemd: Started Session 51 of user root.
Sep  9 00:01:01 node101 systemd: Started Session 52 of user root.
Sep  9 01:01:02 node101 systemd: Started Session 53 of user root.
Sep  9 01:06:52 node101 NetworkManager[3613]: <info>  [1568005612.4339] device (eth0): carrier: link connected
Sep  9 02:00:03 node101 systemd-logind: Removed session 51.
Sep  9 02:00:03 node101 systemd: Removed slice User Slice of root.
--More--(2%)[Press space to continue, 'q' to quit.]
[root@node101.yinzhengjie.org.cn ~]# more -d /var/log/messages   #注意最后一行,除了显示当前页在占该文件的百分比外,还会有一个退出提示

2>.less命令用于一页一页地查看文件或STDIN输出(功能较more更强,man命令底层就调用了less,因此man命令的一些快捷键在less命令上也是可以用的) 

[root@node101.yinzhengjie.org.cn ~]# less --help            #查看该命令的帮助信息

                   SUMMARY OF LESS COMMANDS

      Commands marked with * may be preceded by a number, N.
      Notes in parentheses indicate the behavior if N is given.
      A key preceded by a caret indicates the Ctrl key; thus ^K is ctrl-K.

  h  H                 Display this help.
  q  :q  Q  :Q  ZZ     Exit.
 ---------------------------------------------------------------------------

                           MOVING

  e  ^E  j  ^N  CR  *  Forward  one line   (or N lines).
  y  ^Y  k  ^K  ^P  *  Backward one line   (or N lines).
  f  ^F  ^V  SPACE  *  Forward  one window (or N lines).
  b  ^B  ESC-v      *  Backward one window (or N lines).
  z                 *  Forward  one window (and set window to N).
  w                 *  Backward one window (and set window to N).
  ESC-SPACE         *  Forward  one window, but don't stop at end-of-file.
  d  ^D             *  Forward  one half-window (and set half-window to N).
  u  ^U             *  Backward one half-window (and set half-window to N).
  ESC-)  RightArrow *  Left  one half screen width (or N positions).
  ESC-(  LeftArrow  *  Right one half screen width (or N positions).
  F                    Forward forever; like "tail -f".
  r  ^R  ^L            Repaint screen.
  R                    Repaint screen, discarding buffered input.
        ---------------------------------------------------
        Default "window" is the screen height.
        Default "half-window" is half of the screen height.
 ---------------------------------------------------------------------------

                          SEARCHING

  /pattern          *  Search forward for (N-th) matching line.
  ?pattern          *  Search backward for (N-th) matching line.
  n                 *  Repeat previous search (for N-th occurrence).
  N                 *  Repeat previous search in reverse direction.
  ESC-n             *  Repeat previous search, spanning files.
  ESC-N             *  Repeat previous search, reverse dir. & spanning files.
  ESC-u                Undo (toggle) search highlighting.
  &pattern          *  Display only matching lines
        ---------------------------------------------------
        A search pattern may be preceded by one or more of:
        ^N or !  Search for NON-matching lines.
        ^E or *  Search multiple files (pass thru END OF FILE).
        ^F or @  Start search at FIRST file (for /) or last file (for ?).
        ^K       Highlight matches, but don't move (KEEP position).
        ^R       Don't use REGULAR EXPRESSIONS.
 ---------------------------------------------------------------------------

                           JUMPING

  g  <  ESC-<       *  Go to first line in file (or line N).
  G  >  ESC->       *  Go to last line in file (or line N).
  p  %              *  Go to beginning of file (or N percent into file).
  t                 *  Go to the (N-th) next tag.
  T                 *  Go to the (N-th) previous tag.
  {  (  [           *  Find close bracket } ) ].
  }  )  ]           *  Find open bracket { ( [.
  ESC-^F <c1> <c2>  *  Find close bracket <c2>.
  ESC-^B <c1> <c2>  *  Find open bracket <c1> 
        ---------------------------------------------------
        Each "find close bracket" command goes forward to the close bracket 
          matching the (N-th) open bracket in the top line.
        Each "find open bracket" command goes backward to the open bracket 
          matching the (N-th) close bracket in the bottom line.

  m<letter>            Mark the current position with <letter>.
  '<letter>            Go to a previously marked position.
  ''                   Go to the previous position.
  ^X^X                 Same as '.
        ---------------------------------------------------
        A mark is any upper-case or lower-case letter.
        Certain marks are predefined:
             ^  means  beginning of the file
             $  means  end of the file
 ---------------------------------------------------------------------------

                        CHANGING FILES

  :e [file]            Examine a new file.
  ^X^V                 Same as :e.
  :n                *  Examine the (N-th) next file from the command line.
  :p                *  Examine the (N-th) previous file from the command line.
  :x                *  Examine the first (or N-th) file from the command line.
  :d                   Delete the current file from the command line list.
  =  ^G  :f            Print current file name.
 ---------------------------------------------------------------------------

                    MISCELLANEOUS COMMANDS

  -<flag>              Toggle a command line option [see OPTIONS below].
  --<name>             Toggle a command line option, by name.
  _<flag>              Display the setting of a command line option.
  __<name>             Display the setting of an option, by name.
  +cmd                 Execute the less cmd each time a new file is examined.

  !command             Execute the shell command with $SHELL.
  |Xcommand            Pipe file between current pos & mark X to shell command.
  v                    Edit the current file with $VISUAL or $EDITOR.
  V                    Print version number of "less".
 ---------------------------------------------------------------------------

                           OPTIONS

        Most options may be changed either on the command line,
        or from within less by using the - or -- command.
        Options may be given in one of two forms: either a single
        character preceded by a -, or a name preceded by --.

  -?  ........  --help
                  Display help (from command line).
  -a  ........  --search-skip-screen
                  Search skips current screen.
  -A  ........  --SEARCH-SKIP-SCREEN
                  Search starts just after target line.
  -b [N]  ....  --buffers=[N]
                  Number of buffers.
  -B  ........  --auto-buffers
                  Don't automatically allocate buffers for pipes.
  -c  -C  ....  --clear-screen --CLEAR-SCREEN
                  Repaint by clearing rather than scrolling.
  -d  ........  --dumb
                  Dumb terminal.
  -D [xn.n]  .  --color=xn.n
                  Set screen colors. (MS-DOS only)
  -e  -E  ....  --quit-at-eof  --QUIT-AT-EOF
                  Quit at end of file.
  -f  ........  --force
                  Force open non-regular files.
  -F  ........  --quit-if-one-screen
                  Quit if entire file fits on first screen.
  -g  ........  --hilite-search
                  Highlight only last match for searches.
  -G  ........  --HILITE-SEARCH
                  Don't highlight any matches for searches.
  --old-bot
                  Revert to the old bottom of screen behavior.
  -h [N]  ....  --max-back-scroll=[N]
                  Backward scroll limit.
  -i  ........  --ignore-case
                  Ignore case in searches that do not contain uppercase.
  -I  ........  --IGNORE-CASE
                  Ignore case in all searches.
  -j [N]  ....  --jump-target=[N]
                  Screen position of target lines.
  -J  ........  --status-column
                  Display a status column at left edge of screen.
  -k [file]  .  --lesskey-file=[file]
                  Use a lesskey file.
  -K            --quit-on-intr
                  Exit less in response to ctrl-C.
  -L  ........  --no-lessopen
                  Ignore the LESSOPEN environment variable.
  -m  -M  ....  --long-prompt  --LONG-PROMPT
                  Set prompt style.
  -n  ........  --line-numbers
                  Don't use line numbers.
  -N  ........  --LINE-NUMBERS
                  Use line numbers.
  -o [file]  .  --log-file=[file]
                  Copy to log file (standard input only).
  -O [file]  .  --LOG-FILE=[file]
                  Copy to log file (unconditionally overwrite).
  -p [pattern]  --pattern=[pattern]
                  Start at pattern (from command line).
  -P [prompt]   --prompt=[prompt]
                  Define new prompt.
  -q  -Q  ....  --quiet  --QUIET  --silent --SILENT
                  Quiet the terminal bell.
  -r  -R  ....  --raw-control-chars  --RAW-CONTROL-CHARS
                  Output "raw" control characters.
  -s  ........  --squeeze-blank-lines
                  Squeeze multiple blank lines.
  -S  ........  --chop-long-lines
                  Chop (truncate) long lines rather than wrapping.
  -t [tag]  ..  --tag=[tag]
                  Find a tag.
  -T [tagsfile] --tag-file=[tagsfile]
                  Use an alternate tags file.
  -u  -U  ....  --underline-special  --UNDERLINE-SPECIAL
                  Change handling of backspaces.
  -V  ........  --version
                  Display the version number of "less".
  -w  ........  --hilite-unread
                  Highlight first new line after forward-screen.
  -W  ........  --HILITE-UNREAD
                  Highlight first new line after any forward movement.
  -x [N[,...]]  --tabs=[N[,...]]
                  Set tab stops.
  -X  ........  --no-init
                  Don't use termcap init/deinit strings.
  -y [N]  ....  --max-forw-scroll=[N]
                  Forward scroll limit.
  -z [N]  ....  --window=[N]
                  Set size of window.
  -" [c[c]]  .  --quotes=[c[c]]
                  Set shell quote characters.
  -~  ........  --tilde
                  Don't display tildes after end of file.
  -# [N]  ....  --shift=[N]
                  Horizontal scroll amount (0 = one half screen width)
      ........  --no-keypad
                  Don't send termcap keypad init/deinit strings.
      ........  --follow-name
                  The F command changes files if the input file is renamed.
      ........  --use-backslash
                  Subsequent options use backslash as escape char.


 ---------------------------------------------------------------------------

                          LINE EDITING

        These keys can be used to edit text being entered 
        on the "command line" at the bottom of the screen.

 RightArrow                       ESC-l     Move cursor right one character.
 LeftArrow                        ESC-h     Move cursor left one character.
 ctrl-RightArrow  ESC-RightArrow  ESC-w     Move cursor right one word.
 ctrl-LeftArrow   ESC-LeftArrow   ESC-b     Move cursor left one word.
 HOME                             ESC-0     Move cursor to start of line.
 END                              ESC-$     Move cursor to end of line.
 BACKSPACE                                  Delete char to left of cursor.
 DELETE                           ESC-x     Delete char under cursor.
 ctrl-BACKSPACE   ESC-BACKSPACE             Delete word to left of cursor.
 ctrl-DELETE      ESC-DELETE      ESC-X     Delete word under cursor.
 ctrl-U           ESC (MS-DOS only)         Delete entire line.
 UpArrow                          ESC-k     Retrieve previous command line.
 DownArrow                        ESC-j     Retrieve next command line.
 TAB                                        Complete filename & cycle.
 SHIFT-TAB                        ESC-TAB   Complete filename & reverse cycle.
 ctrl-L                                     Complete filename, list all.


[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# less --help            #查看该命令的帮助信息
[root@node101.yinzhengjie.org.cn ~]# less /var/log/messages
Sep  8 03:41:01 node101 rsyslogd: [origin software="rsyslogd" swVersion="8.24.0-34.el7" x-pid="4104" x-info="http://www.rsyslog.com"] rsyslogd was HUPed
Sep  8 04:01:01 node101 systemd: Started Session 31 of user root.
Sep  8 05:01:01 node101 systemd: Started Session 32 of user root.
Sep  8 06:01:01 node101 systemd: Started Session 33 of user root.
Sep  8 07:01:01 node101 systemd: Started Session 34 of user root.
Sep  8 08:01:01 node101 systemd: Started Session 35 of user root.
Sep  8 08:15:17 node101 chronyd[3566]: Source 193.228.143.12 replaced with 124.108.20.1
Sep  8 09:01:01 node101 systemd: Started Session 36 of user root.
Sep  8 10:01:01 node101 systemd: Started Session 37 of user root.
Sep  8 11:01:01 node101 systemd: Started Session 38 of user root.
Sep  8 12:01:01 node101 systemd: Started Session 39 of user root.
Sep  8 13:00:23 node101 chronyd[3566]: Can't synchronise: no selectable sources
Sep  8 13:01:01 node101 systemd: Started Session 40 of user root.
Sep  8 14:01:01 node101 systemd: Started Session 41 of user root.
Sep  8 15:01:01 node101 systemd: Started Session 42 of user root.
Sep  8 16:01:01 node101 systemd: Started Session 43 of user root.
Sep  8 17:01:02 node101 systemd: Started Session 44 of user root.
Sep  8 18:01:01 node101 systemd: Started Session 45 of user root.
Sep  8 18:10:15 node101 chronyd[3566]: Selected source 45.43.30.59
Sep  8 18:36:16 node101 chronyd[3566]: Source 124.108.20.1 replaced with 139.199.214.202
Sep  8 19:01:01 node101 systemd: Started Session 46 of user root.
Sep  8 20:01:01 node101 systemd: Started Session 47 of user root.
Sep  8 20:37:06 node101 chronyd[3566]: Selected source 139.199.214.202
Sep  8 20:58:19 node101 systemd-logind: Removed session 1.
Sep  8 20:58:19 node101 systemd: Removed slice User Slice of root.
Sep  8 21:01:01 node101 systemd: Created slice User Slice of root.
Sep  8 21:01:01 node101 systemd: Started Session 48 of user root.
Sep  8 21:01:01 node101 systemd: Removed slice User Slice of root.
Sep  8 22:01:01 node101 systemd: Created slice User Slice of root.
Sep  8 22:01:01 node101 systemd: Started Session 49 of user root.
Sep  8 22:01:01 node101 systemd: Removed slice User Slice of root.
Sep  8 23:01:01 node101 systemd: Created slice User Slice of root.
Sep  8 23:01:01 node101 systemd: Started Session 50 of user root.
Sep  8 23:01:01 node101 systemd: Removed slice User Slice of root.
Sep  8 23:04:18 node101 systemd: Starting Cleanup of Temporary Directories...
Sep  8 23:04:18 node101 systemd: Started Cleanup of Temporary Directories.
Sep  8 23:51:00 node101 systemd: Created slice User Slice of root.
Sep  8 23:51:00 node101 systemd-logind: New session 51 of user root.
Sep  8 23:51:00 node101 systemd: Started Session 51 of user root.
Sep  9 00:01:01 node101 systemd: Started Session 52 of user root.
Sep  9 01:01:02 node101 systemd: Started Session 53 of user root.
Sep  9 01:06:52 node101 NetworkManager[3613]: <info>  [1568005612.4339] device (eth0): carrier: link connected
Sep  9 02:00:03 node101 systemd-logind: Removed session 51.
Sep  9 02:00:03 node101 systemd: Removed slice User Slice of root.
/var/log/messages
[root@node101.yinzhengjie.org.cn ~]# less /var/log/messages

 

四.显示文本前或后行内容

1>.head

[root@node101.yinzhengjie.org.cn ~]# head --help      #查看该命令的帮助信息
Usage: head [OPTION]... [FILE]...
Print the first 10 lines of each FILE to standard output.
With more than one FILE, precede each with a header giving the file name.
With no FILE, or when FILE is -, read standard input.

Mandatory arguments to long options are mandatory for short options too.
  -c, --bytes=[-]K         print the first K bytes of each file;
                             with the leading '-', print all but the last
                             K bytes of each file
  -n, --lines=[-]K         print the first K lines instead of the first 10;
                             with the leading '-', print all but the last
                             K lines of each file
  -q, --quiet, --silent    never print headers giving file names
  -v, --verbose            always print headers giving file names
      --help     display this help and exit
      --version  output version information and exit

K may have a multiplier suffix:
b 512, kB 1000, K 1024, MB 1000*1000, M 1024*1024,
GB 1000*1000*1000, G 1024*1024*1024, and so on for T, P, E, Z, Y.

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
For complete documentation, run: info coreutils 'head invocation'
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# head --help              #查看该命令的帮助信息
[root@node101.yinzhengjie.org.cn ~]# seq 100 > f1.txt 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# head f1.txt 
1
2
3
4
5
6
7
8
9
10
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# head -n 5 f1.txt       #只看前5行
1
2
3
4
5
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# head -5 f1.txt          #和上面一样,可以不写"-n",咱们可以简写
1
2
3
4
5
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# head -n 5 f1.txt           #只看前5行
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# echo "jason" | head -c 3      #很显然,只显示了三个字节,没有换行符。
jas[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# echo "尹正杰到此一游" | head -c9   #虽说我们这样可以让其显示9个字节,但是由于Linux默认使用UTF-8编码,因此一个汉字一般情况下默认对应3个字节,最终输出"尹正杰",注意没有换行符哟~
尹正杰[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# echo "jason" | head -c 3      #很显然,只显示了三个字节,没有换行符。
[root@node101.yinzhengjie.org.cn ~]# cat /dev/urandom | tr -dc 'a-zA-Z0-9' | head -c 12    #快速生成12个随机字符
XzTSsQdf5NmX[root@node101.yinzhengjie.org.cn ~]# cat /dev/urandom | tr -dc 'a-zA-Z0-9' | head -c 12
EyUrxFQTelEs[root@node101.yinzhengjie.org.cn ~]# cat /dev/urandom | tr -dc 'a-zA-Z0-9' | head -c 12
FLALxqxAoPGr[root@node101.yinzhengjie.org.cn ~]# cat /dev/urandom | tr -dc 'a-zA-Z0-9' | head -c 12
1r43iy0ofpCO[root@node101.yinzhengjie.org.cn ~]# cat /dev/urandom | tr -dc 'a-zA-Z0-9' | head -c 12
oUVoKrBqbNCm[root@node101.yinzhengjie.org.cn ~]# cat /dev/urandom | tr -dc 'a-zA-Z0-9' | head -c 12
f8gpt0mqSoxk[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat /dev/urandom | tr -dc 'a-zA-Z0-9' | head -c 12    #快速生成12个随机字符

2>.tail

[root@node101.yinzhengjie.org.cn ~]# tail --help          #查看该命令的帮助信息
Usage: tail [OPTION]... [FILE]...
Print the last 10 lines of each FILE to standard output.
With more than one FILE, precede each with a header giving the file name.
With no FILE, or when FILE is -, read standard input.

Mandatory arguments to long options are mandatory for short options too.
  -c, --bytes=K            output the last K bytes; or use -c +K to output
                             bytes starting with the Kth of each file
  -f, --follow[={name|descriptor}]
                           output appended data as the file grows;
                             an absent option argument means 'descriptor'
  -F                       same as --follow=name --retry
  -n, --lines=K            output the last K lines, instead of the last 10;
                             or use -n +K to output starting with the Kth
      --max-unchanged-stats=N
                           with --follow=name, reopen a FILE which has not
                             changed size after N (default 5) iterations
                             to see if it has been unlinked or renamed
                             (this is the usual case of rotated log files);
                             with inotify, this option is rarely useful
      --pid=PID            with -f, terminate after process ID, PID dies
  -q, --quiet, --silent    never output headers giving file names
      --retry              keep trying to open a file if it is inaccessible
  -s, --sleep-interval=N   with -f, sleep for approximately N seconds
                             (default 1.0) between iterations;
                             with inotify and --pid=P, check process P at
                             least once every N seconds
  -v, --verbose            always output headers giving file names
      --help     display this help and exit
      --version  output version information and exit

If the first character of K (the number of bytes or lines) is a '+',
print beginning with the Kth item from the start of each file, otherwise,
print the last K items in the file.  K may have a multiplier suffix:
b 512, kB 1000, K 1024, MB 1000*1000, M 1024*1024,
GB 1000*1000*1000, G 1024*1024*1024, and so on for T, P, E, Z, Y.

With --follow (-f), tail defaults to following the file descriptor, which
means that even if a tail'ed file is renamed, tail will continue to track
its end.  This default behavior is not desirable when you really want to
track the actual name of the file, not the file descriptor (e.g., log
rotation).  Use --follow=name in that case.  That causes tail to track the
named file in a way that accommodates renaming, removal and creation.

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
For complete documentation, run: info coreutils 'tail invocation'
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# tail --help          #查看该命令的帮助信息
[root@node101.yinzhengjie.org.cn ~]# tail f1.txt           #和head命令类似,如果我们不指定显示的行数那默认就显示10行。
91
92
93
94
95
96
97
98
99
100
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# tail -n 3 f1.txt          #显示后3行
98
99
100
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# tail -3 f1.txt            #同上,一种简写形式
98
99
100
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# tail -n 3 f1.txt         #显示后3行
[root@node101.yinzhengjie.org.cn ~]# tail -10f /var/log/messages
Oct 17 21:26:37 node101 chronyd[3584]: Selected source 139.199.214.202
Oct 17 21:26:57 node101 systemd: Created slice User Slice of root.
Oct 17 21:26:57 node101 systemd-logind: New session 4 of user root.
Oct 17 21:26:57 node101 systemd: Started Session 4 of user root.
Oct 17 21:38:42 node101 systemd: Starting Cleanup of Temporary Directories...
Oct 17 21:38:43 node101 systemd: Started Cleanup of Temporary Directories.
Oct 17 22:01:01 node101 systemd: Started Session 5 of user root.
Oct 17 22:26:53 node101 yum[4828]: Installed: 2:vim-filesystem-7.4.629-6.el7.x86_64
Oct 17 22:26:56 node101 yum[4828]: Installed: 2:vim-common-7.4.629-6.el7.x86_64
Oct 17 23:01:01 node101 systemd: Started Session 6 of user root.
尹正杰到此一游



#注意:我们此时开启另一个终端,只要往"/var/log/messages"文件写入东西就可以被监听到哟~
[root@node101.yinzhengjie.org.cn ~]# tail -10f /var/log/messages    #跟踪的是文件描述符,即fd
[root@node101.yinzhengjie.org.cn ~]# tail -F f1.txt              #跟踪文件名,当文件被删除时会有相应的提示信息,当文件被新建后依旧还会监控该文件内容
91
92
93
94
95
96
97
98
99
100
tail: ‘f1.txt’ has become inaccessible: No such file or directory

注意,如果此时我们执行了一些标准错误信息如果使用"-F"参数监听文件的话也会被写进去,比如我们另外开启一个终端执行"rm -f f1.txt"就会出现上面最后一行的错误信息提示哟~
[root@node101.yinzhengjie.org.cn ~]# tail -F f1.txt             #跟踪文件名,当文件被删除时会有相应的提示信息,当文件被新建后依旧还会监控该文件内容
[root@node101.yinzhengjie.org.cn ~]# pidof tail                      #查看某个进程的文件描述符
4962
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ll /proc/`pidof tail`/fd        #同过这种方式就可以看到tail命令对应的文件描述符
total 0
lrwx------. 1 root root 64 Oct 17 23:28 0 -> /dev/pts/0
lrwx------. 1 root root 64 Oct 17 23:28 1 -> /dev/pts/0
lrwx------. 1 root root 64 Oct 17 23:28 2 -> /dev/pts/0
lr-x------. 1 root root 64 Oct 17 23:29 3 -> /root/f1.txt
lr-x------. 1 root root 64 Oct 17 23:28 4 -> anon_inode:inotify
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ll /proc/`pidof tail`/fd        #同过这种方式就可以看到tail命令对应的文件描述符

3>.tailf(和tail -f类似,当文件不增长时并不访问文件)

[root@node101.yinzhengjie.org.cn ~]# tailf /var/log/messages
Oct 17 21:26:57 node101 systemd: Created slice User Slice of root.
Oct 17 21:26:57 node101 systemd-logind: New session 4 of user root.
Oct 17 21:26:57 node101 systemd: Started Session 4 of user root.
Oct 17 21:38:42 node101 systemd: Starting Cleanup of Temporary Directories...
Oct 17 21:38:43 node101 systemd: Started Cleanup of Temporary Directories.
Oct 17 22:01:01 node101 systemd: Started Session 5 of user root.
Oct 17 22:26:53 node101 yum[4828]: Installed: 2:vim-filesystem-7.4.629-6.el7.x86_64
Oct 17 22:26:56 node101 yum[4828]: Installed: 2:vim-common-7.4.629-6.el7.x86_64
Oct 17 23:01:01 node101 systemd: Started Session 6 of user root.
尹正杰到此一游 c
[root@node101.yinzhengjie.org.cn ~]# tailf /var/log/messages

  

五.按列抽取文本cut

1>.查看cut帮助信息

[root@node101.yinzhengjie.org.cn ~]# cut --help          #查看该命令的帮助信息
Usage: cut OPTION... [FILE]...
Print selected parts of lines from each FILE to standard output.

Mandatory arguments to long options are mandatory for short options too.
  -b, --bytes=LIST        select only these bytes
  -c, --characters=LIST   select only these characters
  -d, --delimiter=DELIM   use DELIM instead of TAB for field delimiter
  -f, --fields=LIST       select only these fields;  also print any line
                            that contains no delimiter character, unless
                            the -s option is specified
  -n                      with -b: don't split multibyte characters
      --complement        complement the set of selected bytes, characters
                            or fields
  -s, --only-delimited    do not print lines not containing delimiters
      --output-delimiter=STRING  use STRING as the output delimiter
                            the default is to use the input delimiter
      --help     display this help and exit
      --version  output version information and exit

Use one, and only one of -b, -c or -f.  Each LIST is made up of one
range, or many ranges separated by commas.  Selected input is written
in the same order that it is read, and is written exactly once.
Each range is one of:

  N     N'th byte, character or field, counted from 1
  N-    from N'th byte, character or field, to end of line
  N-M   from N'th to M'th (included) byte, character or field
  -M    from first to M'th (included) byte, character or field

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

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
For complete documentation, run: info coreutils 'cut invocation'
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cut --help          #查看该命令的帮助信息

2>.cut命令案例

[root@node101.yinzhengjie.org.cn ~]# cat /etc/passwd                
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
yinzhengjie:x:1000:1000::/home/yinzhengjie:/bin/bash
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cut -d : -f 1,3,7 /etc/passwd      #我们使用"=d"参数指定分隔符为":",使用"-f"指定我们要获取的列分别为"1,3,7"这三列数据。
root:0:/bin/bash
bin:1:/sbin/nologin
daemon:2:/sbin/nologin
adm:3:/sbin/nologin
lp:4:/sbin/nologin
sync:5:/bin/sync
shutdown:6:/sbin/shutdown
halt:7:/sbin/halt
mail:8:/sbin/nologin
operator:11:/sbin/nologin
games:12:/sbin/nologin
ftp:14:/sbin/nologin
nobody:99:/sbin/nologin
systemd-network:192:/sbin/nologin
dbus:81:/sbin/nologin
polkitd:999:/sbin/nologin
sshd:74:/sbin/nologin
postfix:89:/sbin/nologin
chrony:998:/sbin/nologin
yinzhengjie:1000:/bin/bash
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cut -d : -f 1,3,7 /etc/passwd      #我们使用"=d"参数指定分隔符为":",使用"-f"指定我们要获取的列分别为"1,3,7"这三列数据。
[root@node101.yinzhengjie.org.cn ~]# cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
yinzhengjie:x:1000:1000::/home/yinzhengjie:/bin/bash
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat /etc/passwd | cut -d : -f 1-5  --output-delimiter=#####        #我们可以自定义输出的没列之间的分隔符哟~我这里就用了"#####"来作为分隔符呢!
root#####x#####0#####0#####root
bin#####x#####1#####1#####bin
daemon#####x#####2#####2#####daemon
adm#####x#####3#####4#####adm
lp#####x#####4#####7#####lp
sync#####x#####5#####0#####sync
shutdown#####x#####6#####0#####shutdown
halt#####x#####7#####0#####halt
mail#####x#####8#####12#####mail
operator#####x#####11#####0#####operator
games#####x#####12#####100#####games
ftp#####x#####14#####50#####FTP User
nobody#####x#####99#####99#####Nobody
systemd-network#####x#####192#####192#####systemd Network Management
dbus#####x#####81#####81#####System message bus
polkitd#####x#####999#####998#####User for polkitd
sshd#####x#####74#####74#####Privilege-separated SSH
postfix#####x#####89#####89#####
chrony#####x#####998#####996#####
yinzhengjie#####x#####1000#####1000#####
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat /etc/passwd | cut -d : -f 1-5 --output-delimiter=#####        #我们可以自定义输出的没列之间的分隔符哟~我这里就用了"#####"来作为分隔符呢!
[root@node101.yinzhengjie.org.cn ~]# who
root     pts/0        2019-10-17 21:26 (172.30.1.2)
root     pts/1        2019-10-17 23:14 (172.30.1.2)
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# who | cut -d " " -f 1            #取出当前登陆用户
root
root
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# who | cut -d " " -f 1            #取出当前登陆用户
[root@node101.yinzhengjie.org.cn ~]# ifconfig 
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.30.1.101  netmask 255.255.255.0  broadcast 172.30.1.255
        inet6 fe80::21c:42ff:fefb:c98a  prefixlen 64  scopeid 0x20<link>
        ether 00:1c:42:fb:c9:8a  txqueuelen 1000  (Ethernet)
        RX packets 25774  bytes 26008520 (24.8 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 9045  bytes 2050740 (1.9 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 64  bytes 5792 (5.6 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 64  bytes 5792 (5.6 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ifconfig eth0 | head -2 | tail -1 | tr -s " " | cut  -d " " -f 3    #使用cut取出IP地址
172.30.1.101
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ifconfig eth0 | head -2 | tail -1 | tr -s " " | cut -d " " -f 3    #使用cut取出IP地址
[root@node101.yinzhengjie.org.cn ~]# cat /etc/passwd | cut  -c 1-15      #只取出前15个字符
root:x:0:0:root
bin:x:1:1:bin:/
daemon:x:2:2:da
adm:x:3:4:adm:/
lp:x:4:7:lp:/va
sync:x:5:0:sync
shutdown:x:6:0:
halt:x:7:0:halt
mail:x:8:12:mai
operator:x:11:0
games:x:12:100:
ftp:x:14:50:FTP
nobody:x:99:99:
systemd-network
dbus:x:81:81:Sy
polkitd:x:999:9
sshd:x:74:74:Pr
postfix:x:89:89
chrony:x:998:99
yinzhengjie:x:1
[root@node101.yinzhengjie.org.cn ~]# 
root@node101.yinzhengjie.org.cn ~]# cat /etc/passwd | cut -c 1-15      #只取出前15个字符

 

六.合并文件paste

1>.查看帮助信息

[root@node101.yinzhengjie.org.cn ~]# paste --help
Usage: paste [OPTION]... [FILE]...
Write lines consisting of the sequentially corresponding lines from
each FILE, separated by TABs, to standard output.
With no FILE, or when FILE is -, read standard input.

Mandatory arguments to long options are mandatory for short options too.
  -d, --delimiters=LIST   reuse characters from LIST instead of TABs
  -s, --serial            paste one file at a time instead of in parallel
      --help     display this help and exit
      --version  output version information and exit

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
For complete documentation, run: info coreutils 'paste invocation'
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# paste --help

2>.paste命令案例

[root@node101.yinzhengjie.org.cn ~]# seq 5 > f1.txt 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat f1.txt 
1
2
3
4
5
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# echo {a..g} | tr ' ' '\n' > f2.txt 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat f2.txt 
a
b
c
d
e
f
g
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# paste f1.txt f2.txt             #横向合并2个内容,若不指定分隔符,默认以TAB制表符进行分割。
1       a
2       b
3       c
4       d
5       e
        f
        g
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# paste f1.txt f2.txt | cat -A          #不难发现,paste默认以空格进行分割2个文件行内容
1^Ia$
2^Ib$
3^Ic$
4^Id$
5^Ie$
^If$
^Ig$
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# paste f1.txt f2.txt             #横向合并2个内容,若不指定分隔符,默认以TAB制表符进行分割。
[root@node101.yinzhengjie.org.cn ~]# paste f1.txt f2.txt  | cat -A       #默认以TAB制表符作为分隔符
1^Ia$
2^Ib$
3^Ic$
4^Id$
5^Ie$
^If$
^Ig$
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# paste -d ":" f1.txt f2.txt          #我们这里使用"-d"选项指定分隔符为":"
1:a
2:b
3:c
4:d
5:e
:f
:g
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# paste -d ":" f1.txt f2.txt         #我们这里使用"-d"选项指定分隔符为":"
[root@node101.yinzhengjie.org.cn ~]# cat f1.txt 
1
2
3
4
5
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat f2.txt  
a
b
c
d
e
f
g
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# paste -s  f1.txt f2.txt         #将每个文件的内容合并成一行显示,每行分隔符不指定默认为TAB制表符
1       2       3       4       5
a       b       c       d       e       f       g
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# paste -s f1.txt f2.txt | cat -A
1^I2^I3^I4^I5$
a^Ib^Ic^Id^Ie^If^Ig$
[root@node101.yinzhengjie.org.cn ~]#
[root@node101.yinzhengjie.org.cn ~]# paste -d ":" f1.txt f2.txt   -s    #这里现实制定了分隔符为":"
1:2:3:4:5
a:b:c:d:e:f:g
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# paste -s f1.txt f2.txt           #将每个文件的内容合并成一行显示,每行分隔符不指定默认为TAB制表符

 

七.分析文本的工具

1>.文本数据统计:wc命令(可用于统计文件的行总数,单词总数,字节总数和字符总数,以及对文件或STDIN中的数据统计)

[root@node101.yinzhengjie.org.cn ~]# wc  --help      #查看该命令的帮助信息
Usage: wc [OPTION]... [FILE]...
  or:  wc [OPTION]... --files0-from=F
Print newline, word, and byte counts for each FILE, and a total line if
more than one FILE is specified.  With no FILE, or when FILE is -,
read standard input.  A word is a non-zero-length sequence of characters
delimited by white space.
The options below may be used to select which counts are printed, always in
the following order: newline, word, character, byte, maximum line length.
  -c, --bytes            print the byte counts
  -m, --chars            print the character counts
  -l, --lines            print the newline counts
      --files0-from=F    read input from the files specified by
                           NUL-terminated names in file F;
                           If F is - then read names from standard input
  -L, --max-line-length  print the length of the longest line
  -w, --words            print the word counts
      --help     display this help and exit
      --version  output version information and exit

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
For complete documentation, run: info coreutils 'wc invocation'
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# wc --help         #查看该命令的帮助信息
[root@node101.yinzhengjie.org.cn ~]# cat hadoop.txt 
The Apache Hadoop software library is a framework that allows for the distributed processing of large data sets across clusters of computers using simple programming models. It is designed to scale up from single servers to thousands of machines, each offering local computation and storage. Rather than rely on hardware to deliver high-availability, the library itself is designed to detect and handle failures at the application layer, so delivering a highly-available service on top of a cluster of computers, each of which may be prone to failures.
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# wc hadoop.txt         #注意输出的4列参数表示的意思,第一列表示该文件共有多少行,第二列表示该文件共计有多少个单词,第三列表示该文件共有多少字节,第四列表示该文件的名称。
  1  87 553 hadoop.txt
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# wc hadoop.txt       #注意输出的4列参数表示的意思,第一列表示该文件共有多少行,第二列表示该文件共计有多少个单词,第三列表示该文件共有多少字节,第四列表示该文件的名称。
[root@node101.yinzhengjie.org.cn ~]# cat f1.txt 
1 linux
2 hadoop
3 python
4 java c++
5 golang
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# wc f1.txt 
 5 11 46 f1.txt
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# wc  -l f1.txt       #只计数行数
5 f1.txt
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# wc -l f1.txt       #只计数行数
[root@node101.yinzhengjie.org.cn ~]# cat f1.txt 
1 linux
2 hadoop
3 python
4 java c++
5 golang
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# wc f1.txt 
 5 11 46 f1.txt
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# wc  -w f1.txt        #只计数单词总数
11 f1.txt
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# wc -w f1.txt       #只计数单词总数
[root@node101.yinzhengjie.org.cn ~]# cat f1.txt 
1 linux
2 hadoop
3 python
4 java c++
5 golang
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# wc f1.txt 
 5 11 46 f1.txt
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# wc  -c f1.txt          #只计数字节总数
46 f1.txt
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# wc -c f1.txt       #只计数字节总数
[root@node101.yinzhengjie.org.cn ~]# cat f1.txt 
1 linux
2 hadoop
3 python
4 java c++
5 golang
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# wc f1.txt 
 5 11 46 f1.txt
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# wc  -m f1.txt        #只计数字符总数
46 f1.txt
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# wc -m f1.txt       #只计数字符总数,注意如果咱们在文本中存在汉字,字符数和字节数是不一样的哟!这涉及到编码的知识。
[root@node101.yinzhengjie.org.cn ~]# cat f1.txt 
1 linux
2 hadoop
3 python
4 java c++
5 golang
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# wc f1.txt 
 5 11 46 f1.txt
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# wc  -L f1.txt        #只显示文件中最长行的长度
10 f1.txt
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# wc -L f1.txt       #只显示文件中最长行的长度

2>.整理文本:sort命令(把整理过的文本显示在STDOUT,不改变原始文件)和uniq命令(从输入中删除前后相接的重复的行)

[root@node101.yinzhengjie.org.cn ~]# sort --help        #显示该命令的帮助信息
Usage: sort [OPTION]... [FILE]...
  or:  sort [OPTION]... --files0-from=F
Write sorted concatenation of all FILE(s) to standard output.

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

  -b, --ignore-leading-blanks  ignore leading blanks
  -d, --dictionary-order      consider only blanks and alphanumeric characters
  -f, --ignore-case           fold lower case to upper case characters
  -g, --general-numeric-sort  compare according to general numerical value
  -i, --ignore-nonprinting    consider only printable characters
  -M, --month-sort            compare (unknown) < 'JAN' < ... < 'DEC'
  -h, --human-numeric-sort    compare human readable numbers (e.g., 2K 1G)
  -n, --numeric-sort          compare according to string numerical value
  -R, --random-sort           sort by random hash of keys
      --random-source=FILE    get random bytes from FILE
  -r, --reverse               reverse the result of comparisons
      --sort=WORD             sort according to WORD:
                                general-numeric -g, human-numeric -h, month -M,
                                numeric -n, random -R, version -V
  -V, --version-sort          natural sort of (version) numbers within text

Other options:

      --batch-size=NMERGE   merge at most NMERGE inputs at once;
                            for more use temp files
  -c, --check, --check=diagnose-first  check for sorted input; do not sort
  -C, --check=quiet, --check=silent  like -c, but do not report first bad line
      --compress-program=PROG  compress temporaries with PROG;
                              decompress them with PROG -d
      --debug               annotate the part of the line used to sort,
                              and warn about questionable usage to stderr
      --files0-from=F       read input from the files specified by
                            NUL-terminated names in file F;
                            If F is - then read names from standard input
  -k, --key=KEYDEF          sort via a key; KEYDEF gives location and type
  -m, --merge               merge already sorted files; do not sort
  -o, --output=FILE         write result to FILE instead of standard output
  -s, --stable              stabilize sort by disabling last-resort comparison
  -S, --buffer-size=SIZE    use SIZE for main memory buffer
  -t, --field-separator=SEP  use SEP instead of non-blank to blank transition
  -T, --temporary-directory=DIR  use DIR for temporaries, not $TMPDIR or /tmp;
                              multiple options specify multiple directories
      --parallel=N          change the number of sorts run concurrently to N
  -u, --unique              with -c, check for strict ordering;
                              without -c, output only the first of an equal run
  -z, --zero-terminated     end lines with 0 byte, not newline
      --help     display this help and exit
      --version  output version information and exit

KEYDEF is F[.C][OPTS][,F[.C][OPTS]] for start and stop position, where F is a
field number and C a character position in the field; both are origin 1, and
the stop position defaults to the line's end.  If neither -t nor -b is in
effect, characters in a field are counted from the beginning of the preceding
whitespace.  OPTS is one or more single-letter ordering options [bdfgiMhnRrV],
which override global ordering options for that key.  If no key is given, use
the entire line as the key.

SIZE may be followed by the following multiplicative suffixes:
% 1% of memory, b 1, K 1024 (default), and so on for M, G, T, P, E, Z, Y.

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

*** WARNING ***
The locale specified by the environment affects sort order.
Set LC_ALL=C to get the traditional sort order that uses
native byte values.

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
For complete documentation, run: info coreutils 'sort invocation'
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# sort --help        #显示该命令的帮助信息
[root@node101.yinzhengjie.org.cn ~]# cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
yinzhengjie:x:1000:1000::/home/yinzhengjie:/bin/bash
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# sort /etc/passwd    #默认会按照每一行的第一个字符进行排序,主要根据ASCII编码表顺序来排序。
adm:x:3:4:adm:/var/adm:/sbin/nologin
bin:x:1:1:bin:/bin:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
halt:x:7:0:halt:/sbin:/sbin/halt
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
root:x:0:0:root:/root:/bin/bash
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
yinzhengjie:x:1000:1000::/home/yinzhengjie:/bin/bash
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# sort /etc/passwd     #默认会按照每一行的第一个字符进行排序,主要根据ASCII编码表顺序来排序。
[root@node101.yinzhengjie.org.cn ~]# cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
yinzhengjie:x:1000:1000::/home/yinzhengjie:/bin/bash
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# sort -t ":" -k 3 -n /etc/passwd      #"-t"参数表示指定分隔符,我这里指定冒号(":")为分隔符,"-k"表示指定以那一列进行排序,我这里指定基于用户的uid进行排序(即第3列),"-n"参数表示将第三列当初数字(number)进行比较,若不加该参数默认以字符串进行比较。
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
yinzhengjie:x:1000:1000::/home/yinzhengjie:/bin/bash
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# sort -t ":" -k 3 -n /etc/passwd      #"-t"参数表示指定分隔符,我这里指定冒号(":")为分隔符,"-k"表示指定以那一列进行排序,我这里指定基于用户的uid进行排序(即第3列),"-n"参数表示将第三列当初数字(number)进行比较,若不加该参数默认以字符串进行比较。
[root@node101.yinzhengjie.org.cn ~]# cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
yinzhengjie:x:1000:1000::/home/yinzhengjie:/bin/bash
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# sort -t ":" -k 3 -n /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
yinzhengjie:x:1000:1000::/home/yinzhengjie:/bin/bash
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# sort -t ":" -k 3 -nr /etc/passwd      #注意,"-r"选项表示降序,如果不加该选项表示升序排列。
yinzhengjie:x:1000:1000::/home/yinzhengjie:/bin/bash
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
halt:x:7:0:halt:/sbin:/sbin/halt
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
sync:x:5:0:sync:/sbin:/bin/sync
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
bin:x:1:1:bin:/bin:/sbin/nologin
root:x:0:0:root:/root:/bin/bash
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# sort -t ":" -k 3 -nr /etc/passwd      #注意,"-r"选项表示降序,如果不加该选项表示升序排列。
[root@node101.yinzhengjie.org.cn ~]# seq 10
1
2
3
4
5
6
7
8
9
10
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# seq 10 | sort -nr      #按数字倒叙排列
10
9
8
7
6
5
4
3
2
1
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# seq 10 | sort -R        #随机排列
6
10
3
5
4
1
8
2
9
7
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# seq 10 | sort -R                #随机排列
[root@node101.yinzhengjie.org.cn ~]# seq 10 | sort -R| head -1      #模拟随机抽取一个数字
10
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# seq 10 | sort -R| head -1
3
[root@node101.yinzhengjie.org.cn ~]# seq 10 | sort -R| head -1
6
[root@node101.yinzhengjie.org.cn ~]# seq 10 | sort -R| head -1
6
[root@node101.yinzhengjie.org.cn ~]# seq 10 | sort -R| head -1
7
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# seq 10 | sort -R| head -1           #模拟随机抽取一个数字
[root@node101.yinzhengjie.org.cn ~]# df 
Filesystem                   1K-blocks    Used Available Use% Mounted on
/dev/mapper/VolGroup-lv_root  51474912 1449844  47387244   3% /
devtmpfs                        927056       0    927056   0% /dev
tmpfs                           938788       0    938788   0% /dev/shm
tmpfs                           938788    9112    929676   1% /run
tmpfs                           938788       0    938788   0% /sys/fs/cgroup
/dev/sda1                       487634  115872    342066  26% /boot
/dev/mapper/VolGroup-lv_home  11778300  596844  10560096   6% /home
tmpfs                           187760       0    187760   0% /run/user/0
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# df | tr -s ' ' '%' | cut -d "%" -f 5 | tr -dc '[0-9]\n' | sort -nr      #对磁盘空间使用率进行排序
26
6
3
1
0
0
0
0

[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# df | tr -s ' ' '%' | cut -d "%" -f 5 | tr -dc '[0-9]\n' | sort -nr      #对磁盘空间使用率进行排序
[root@node101.yinzhengjie.org.cn ~]# uniq --help                #查看该命令的帮助信息
Usage: uniq [OPTION]... [INPUT [OUTPUT]]
Filter adjacent matching lines from INPUT (or standard input),
writing to OUTPUT (or standard output).

With no options, matching lines are merged to the first occurrence.

Mandatory arguments to long options are mandatory for short options too.
  -c, --count           prefix lines by the number of occurrences
  -d, --repeated        only print duplicate lines, one for each group
  -D, --all-repeated[=METHOD]  print all duplicate lines
                          groups can be delimited with an empty line
                          METHOD={none(default),prepend,separate}
  -f, --skip-fields=N   avoid comparing the first N fields
      --group[=METHOD]  show all items, separating groups with an empty line
                          METHOD={separate(default),prepend,append,both}
  -i, --ignore-case     ignore differences in case when comparing
  -s, --skip-chars=N    avoid comparing the first N characters
  -u, --unique          only print unique lines
  -z, --zero-terminated  end lines with 0 byte, not newline
  -w, --check-chars=N   compare no more than N characters in lines
      --help     display this help and exit
      --version  output version information and exit

A field is a run of blanks (usually spaces and/or TABs), then non-blank
characters.  Fields are skipped before chars.

Note: 'uniq' does not detect repeated lines unless they are adjacent.
You may want to sort the input first, or use 'sort -u' without 'uniq'.
Also, comparisons honor the rules specified by 'LC_COLLATE'.

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
For complete documentation, run: info coreutils 'uniq invocation'
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# uniq --help                      #查看该命令的帮助信息
[root@node101.yinzhengjie.org.cn ~]# cat f3.txt
a
b
a
aa
bb
bb
bb
ccc
ccc
ccc
d
eee
eee
eee
eee
eee
a
eee
f
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# uniq f3.txt           #该命令会默认删除前后相接重复的行
a
b
a
aa
bb
ccc
d
eee
a
eee
f
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# uniq f3.txt                       #该命令会默认删除前后相接重复的行
[root@node101.yinzhengjie.org.cn ~]# cat f3.txt
a
b
a
aa
bb
bb
bb
ccc
ccc
ccc
d
eee
eee
eee
eee
eee
a
eee
f
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# uniq -c f3.txt           #除了删除前后相接重复的行外,这里的"-c"选项还可以显示相邻行出现重复的频次
      1 a
      1 b
      1 a
      1 aa
      3 bb
      3 ccc
      1 d
      5 eee
      1 a
      1 eee
      1 f
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# uniq -c f3.txt                     #除了删除前后相接重复的行外,这里的"-c"选项还可以显示相邻行出现重复的频次
[root@node101.yinzhengjie.org.cn ~]# cat f3.txt    
a
b
a
aa
bb
bb
bb
ccc
ccc
ccc
d
eee
eee
eee
eee
eee
a
eee
f
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# uniq -c f3.txt | sort -nr      #对相邻行之间出现对频次进行一个降序排列
      5 eee
      3 ccc
      3 bb
      1 f
      1 eee
      1 d
      1 b
      1 aa
      1 a
      1 a
      1 a
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# uniq -c f3.txt | sort -nr              #对相邻行之间出现对频次进行一个降序排列
[root@node101.yinzhengjie.org.cn ~]# cat f3.txt               
a
b
a
aa
bb
bb
bb
ccc
ccc
ccc
d
eee
eee
eee
eee
eee
a
eee
f
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# uniq -d f3.txt        #只显示相邻行出现重复的行
bb
ccc
eee
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# uniq -d -c f3.txt 
      3 bb
      3 ccc
      5 eee
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# uniq -d f3.txt                     #只显示相邻行出现重复的行
[root@node101.yinzhengjie.org.cn ~]# cat f3.txt   
a
b
a
aa
bb
bb
bb
ccc
ccc
ccc
d
eee
eee
eee
eee
eee
a
eee
f
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# uniq -u f3.txt            #只显示相邻行没有出现重复的行
a
b
a
aa
d
a
eee
f
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# uniq -u f3.txt                     #只显示相邻行没有出现重复的行 
[root@node101.yinzhengjie.org.cn ~]# ll
total 20
drwxr-xr-x. 2 root root 4096 Oct 18 03:26 data
-rw-r--r--. 1 root root   46 Oct 18 02:24 f1.txt
-rw-r--r--. 1 root root   14 Oct 18 02:04 f2.txt
-rw-r--r--. 1 root root   60 Oct 18 03:09 f3.txt
-rw-r--r--. 1 root root  553 Oct 18 02:20 hadoop.txt
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ll data/
total 12
-rw-r--r--. 1 root root  0 Oct 18 03:26 6.txt
-rw-r--r--. 1 root root  0 Oct 18 03:26 7.txt
-rw-r--r--. 1 root root  0 Oct 18 03:26 8.txt
-rw-r--r--. 1 root root 46 Oct 18 03:25 f1.txt
-rw-r--r--. 1 root root 14 Oct 18 03:25 f2.txt
-rw-r--r--. 1 root root 60 Oct 18 03:25 f3.txt
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ls /root/ > /tmp/root.log
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ls /root/data/ > /tmp/data.log
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat /tmp/root.log /tmp/data.log | sort  | uniq -d        #统计2个目录出现文件名相同的行
f1.txt
f2.txt
f3.txt
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# sort /tmp/data.log /tmp/root.log  | uniq -d
f1.txt
f2.txt
f3.txt
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ls /root/ /root/data/ -1 | sort  | uniq -d                  #可以很轻松统计两个目录中出现同名的文件名
f1.txt
f2.txt
f3.txt
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ls /root/ /root/data/ -1 | sort | uniq -d     #可以很轻松统计两个目录中出现同名的文件名
[root@node101.yinzhengjie.org.cn ~]# ls
data  f1.txt  f2.txt  f3.txt  hadoop.txt
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ls data/
6.txt  7.txt  8.txt  f1.txt  f2.txt  f3.txt
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ls /root/ /root/data/ -1 | sort  | uniq -u    #取出两个目录中不同的文件列表

6.txt
7.txt
8.txt
data
hadoop.txt
/root/:
/root/data/:
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ls /root/ /root/data/ -1 | sort | uniq -u    #取出两个目录中不同的文件列表
运维面试题一:取出Nginx访问日志中"/var/log/access_log"中访问量Top 10.
    参考案例:[root@node101.yinzhengjie.org.cn ~]# cut -d " " -f 1 /var/log/access_log | sort | uniq -c | sort -nr | head -n 10

运维面试题二:取出"login.txt"文件中用户登陆失败IP最高的Top 10.
  参考案例:[root@node101.yinzhengjie.org.cn ~]# lastb -f login.txt | tr -s " " | cut  -d " " -f 3 | sort | uniq -c | sort -nr | head

运维面试题三:
取出"login.txt"文件中用户登陆失败尝试最多用户的Top 5.
  参考案例:[root@node101.yinzhengjie.org.cn ~]# lastb -f login.txt | cut  -d " " -f 1 | sort | uniq -c | sort -nr | head -5

3>.比较文件:diff(比较两个文件的区别,命令的输出被保存在一种叫做"补丁"的文件中)和patch(复制在其他文件中进行的改变,要谨慎使用)

[root@node101.yinzhengjie.org.cn ~]# diff --help          #查看该命令的帮助信息
Usage: diff [OPTION]... FILES
Compare FILES line by line.

Mandatory arguments to long options are mandatory for short options too.
      --normal                  output a normal diff (the default)
  -q, --brief                   report only when files differ
  -s, --report-identical-files  report when two files are the same
  -c, -C NUM, --context[=NUM]   output NUM (default 3) lines of copied context
  -u, -U NUM, --unified[=NUM]   output NUM (default 3) lines of unified context
  -e, --ed                      output an ed script
  -n, --rcs                     output an RCS format diff
  -y, --side-by-side            output in two columns
  -W, --width=NUM               output at most NUM (default 130) print columns
      --left-column             output only the left column of common lines
      --suppress-common-lines   do not output common lines

  -p, --show-c-function         show which C function each change is in
  -F, --show-function-line=RE   show the most recent line matching RE
      --label LABEL             use LABEL instead of file name
                                  (can be repeated)

  -t, --expand-tabs             expand tabs to spaces in output
  -T, --initial-tab             make tabs line up by prepending a tab
      --tabsize=NUM             tab stops every NUM (default 8) print columns
      --suppress-blank-empty    suppress space or tab before empty output lines
  -l, --paginate                pass output through 'pr' to paginate it

  -r, --recursive                 recursively compare any subdirectories found
      --no-dereference            don't follow symbolic links
  -N, --new-file                  treat absent files as empty
      --unidirectional-new-file   treat absent first files as empty
      --ignore-file-name-case     ignore case when comparing file names
      --no-ignore-file-name-case  consider case when comparing file names
  -x, --exclude=PAT               exclude files that match PAT
  -X, --exclude-from=FILE         exclude files that match any pattern in FILE
  -S, --starting-file=FILE        start with FILE when comparing directories
      --from-file=FILE1           compare FILE1 to all operands;
                                    FILE1 can be a directory
      --to-file=FILE2             compare all operands to FILE2;
                                    FILE2 can be a directory

  -i, --ignore-case               ignore case differences in file contents
  -E, --ignore-tab-expansion      ignore changes due to tab expansion
  -Z, --ignore-trailing-space     ignore white space at line end
  -b, --ignore-space-change       ignore changes in the amount of white space
  -w, --ignore-all-space          ignore all white space
  -B, --ignore-blank-lines        ignore changes where lines are all blank
  -I, --ignore-matching-lines=RE  ignore changes where all lines match RE

  -a, --text                      treat all files as text
      --strip-trailing-cr         strip trailing carriage return on input

  -D, --ifdef=NAME                output merged file with '#ifdef NAME' diffs
      --GTYPE-group-format=GFMT   format GTYPE input groups with GFMT
      --line-format=LFMT          format all input lines with LFMT
      --LTYPE-line-format=LFMT    format LTYPE input lines with LFMT
    These format options provide fine-grained control over the output
      of diff, generalizing -D/--ifdef.
    LTYPE is 'old', 'new', or 'unchanged'.  GTYPE is LTYPE or 'changed'.
    GFMT (only) may contain:
      %<  lines from FILE1
      %>  lines from FILE2
      %=  lines common to FILE1 and FILE2
      %[-][WIDTH][.[PREC]]{doxX}LETTER  printf-style spec for LETTER
        LETTERs are as follows for new group, lower case for old group:
          F  first line number
          L  last line number
          N  number of lines = L-F+1
          E  F-1
          M  L+1
      %(A=B?T:E)  if A equals B then T else E
    LFMT (only) may contain:
      %L  contents of line
      %l  contents of line, excluding any trailing newline
      %[-][WIDTH][.[PREC]]{doxX}n  printf-style spec for input line number
    Both GFMT and LFMT may contain:
      %%  %
      %c'C'  the single character C
      %c'\OOO'  the character with octal code OOO
      C    the character C (other characters represent themselves)

  -d, --minimal            try hard to find a smaller set of changes
      --horizon-lines=NUM  keep NUM lines of the common prefix and suffix
      --speed-large-files  assume large files and many scattered small changes

      --help               display this help and exit
  -v, --version            output version information and exit

FILES are 'FILE1 FILE2' or 'DIR1 DIR2' or 'DIR FILE...' or 'FILE... DIR'.
If --from-file or --to-file is given, there are no restrictions on FILE(s).
If a FILE is '-', read standard input.
Exit status is 0 if inputs are the same, 1 if different, 2 if trouble.

Report bugs to: bug-diffutils@gnu.org
GNU diffutils home page: <http://www.gnu.org/software/diffutils/>
General help using GNU software: <http://www.gnu.org/gethelp/>
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# diff --help          #查看该命令的帮助信息 
[root@node101.yinzhengjie.org.cn ~]# cat a.txt 
a
b
c
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat b.txt  
a
bb
ccc
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# diff a.txt b.txt       #显示2个文件不同的行。
2,3c2,3
< b
< c
---
> bb
> ccc
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# diff a.txt b.txt       #显示2个文件不同的行。
[root@node101.yinzhengjie.org.cn ~]# cat a.txt 
a
b
c
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat b.txt 
a
bb
ccc
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# diff -u a.txt b.txt    #可以看到更加详细的对比
--- a.txt       2019-10-18 04:06:11.929873028 -0400      #下面带有"-"表示的是a.txt
+++ b.txt       2019-10-18 04:07:04.243307997 -0400      #下面带有"+"表示的是b.txt
@@ -1,3 +1,3 @@
 a
-b
-c
+bb
+ccc
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# diff -u a.txt b.txt    #可以看到更加详细的对比
[root@node101.yinzhengjie.org.cn ~]# patch --help          #查看该命令的帮助信息
Usage: patch [OPTION]... [ORIGFILE [PATCHFILE]]

Input options:

  -p NUM  --strip=NUM  Strip NUM leading components from file names.
  -F LINES  --fuzz LINES  Set the fuzz factor to LINES for inexact matching.
  -l  --ignore-whitespace  Ignore white space changes between patch and input.

  -c  --context  Interpret the patch as a context difference.
  -e  --ed  Interpret the patch as an ed script.
  -n  --normal  Interpret the patch as a normal difference.
  -u  --unified  Interpret the patch as a unified difference.

  -N  --forward  Ignore patches that appear to be reversed or already applied.
  -R  --reverse  Assume patches were created with old and new files swapped.

  -i PATCHFILE  --input=PATCHFILE  Read patch from PATCHFILE instead of stdin.

Output options:

  -o FILE  --output=FILE  Output patched files to FILE.
  -r FILE  --reject-file=FILE  Output rejects to FILE.

  -D NAME  --ifdef=NAME  Make merged if-then-else output using NAME.
  --merge  Merge using conflict markers instead of creating reject files.
  -E  --remove-empty-files  Remove output files that are empty after patching.

  -Z  --set-utc  Set times of patched files, assuming diff uses UTC (GMT).
  -T  --set-time  Likewise, assuming local time.

  --quoting-style=WORD   output file names using quoting style WORD.
    Valid WORDs are: literal, shell, shell-always, c, escape.
    Default is taken from QUOTING_STYLE env variable, or 'shell' if unset.

Backup and version control options:

  -b  --backup  Back up the original contents of each file.
  --backup-if-mismatch  Back up if the patch does not match exactly.
  --no-backup-if-mismatch  Back up mismatches only if otherwise requested.

  -V STYLE  --version-control=STYLE  Use STYLE version control.
        STYLE is either 'simple', 'numbered', or 'existing'.
  -B PREFIX  --prefix=PREFIX  Prepend PREFIX to backup file names.
  -Y PREFIX  --basename-prefix=PREFIX  Prepend PREFIX to backup file basenames.
  -z SUFFIX  --suffix=SUFFIX  Append SUFFIX to backup file names.

  -g NUM  --get=NUM  Get files from RCS etc. if positive; ask if negative.

Miscellaneous options:

  -t  --batch  Ask no questions; skip bad-Prereq patches; assume reversed.
  -f  --force  Like -t, but ignore bad-Prereq patches, and assume unreversed.
  -s  --quiet  --silent  Work silently unless an error occurs.
  --verbose  Output extra information about the work being done.
  --dry-run  Do not actually change any files; just print what would happen.
  --posix  Conform to the POSIX standard.

  -d DIR  --directory=DIR  Change the working directory to DIR first.
  --reject-format=FORMAT  Create 'context' or 'unified' rejects.
  --binary  Read and write data in binary mode.
  --read-only=BEHAVIOR  How to handle read-only input files: 'ignore' that they
                        are read-only, 'warn' (default), or 'fail'.
  -x NUM  --debug=NUM  Set internal debugging flags.

  -v  --version  Output version info.
  --help  Output this help.

Report bugs to <bug-patch@gnu.org>.
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# patch --help          #查看该命令的帮助信息
[root@node101.yinzhengjie.org.cn ~]# cat a.txt 
a
b
c
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat b.txt 
a
bb
ccc
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# diff -u a.txt b.txt > diff.log      #我们将文件的不同之处保存到一个中间文件中
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# rm -f b.txt                  #此时我们将“b.txt”文件删除掉
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# patch -b a.txt diff.log           #我们使用patch命令可以找回文件,"-b"参数表示对a.txt文件进行备份,因为我们使用patch命令基于diff.log文件恢复b.txt文件内容会覆盖a.txt文件内容。
patching file a.txt
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# ll
total 12
-rw-r--r--. 1 root root   9 Oct 18 04:16 a.txt
-rw-r--r--. 1 root root   6 Oct 18 04:06 a.txt.orig
-rw-r--r--. 1 root root 126 Oct 18 04:14 diff.log
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat a.txt                    #这是原来"b.txt"文件内容
a
bb
ccc
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat a.txt.orig                  #这是原来"a.txt"文件内容的备份
a
b
c
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# mv a.txt b.txt
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# mv a.txt.orig a.txt
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat a.txt 
a
b
c
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat b.txt 
a
bb
ccc
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# patch -b a.txt diff.log           #我们使用patch命令可以找回文件,"-b"参数表示对a.txt文件进行备份,因为我们使用patch命令基于diff.log文件恢复b.txt文件内容会覆盖a.txt文件内容。

 

八.小试牛刀

1>.找出ifconfig “网卡名” 命令结果中本机的IPv4地址 
2>.查出分区空间使用率的最大百分比值
3>.查出用户UID最大值的用户名、UID及shell类型
4>.查出/tmp的权限,以数字方式显示
5>.统计当前连接本机的每个远程主机IP的连接数,并按从大到小排序

 

posted @ 2019-08-29 20:50  尹正杰  阅读(398)  评论(0编辑  收藏  举报