Linux文本工具-cat-cut-paste;文本分析-sort-wc-uniq

 

1.1 查看文本文件内容  cat

1.1.1 cat可以查看文本内容

  

cat [OPTION]... [FILE]...

常见选项

-E:    显示行结束符$
-A:    显示所有控制符
-n:    对显示出的每一行进行编号
-b:    非空行编号
-s:    压缩连续的空行成一行

1.1.2  nl

显示行号,相当于cat  -b

[10:00:37 root@centos8 ~]#nl /etc/fstab 
       
     1    #
     2    # /etc/fstab
     3    # Created by anaconda on Tue Apr  5 08:42:34 2022
     4    #
     5    # Accessible filesystems, by reference, are maintained under '/dev/disk/'.
     6    # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.
     7    #
     8    # After editing this file, run 'systemctl daemon-reload' to update systemd
     9    # units generated from this file.
    10    #
    11    UUID=7a405199-a904-4290-89a3-72992b4ad489 /                       xfs     defaults        0 0
    12    UUID=6c58d4aa-0c80-4e0b-b372-0a2187033d23 /boot                   ext4    defaults        1 2
    13    UUID=2dd6f870-9df8-47ec-a4fc-b8a01878f4e0 /data                   xfs     defaults        0 0
    14    UUID=27f69466-124d-4124-aab8-85f247d76d57 none                    swap    defaults        0 0
[10:00:46 root@centos8 ~]#cat -b /etc/fstab 

     1    #
     2    # /etc/fstab
     3    # Created by anaconda on Tue Apr  5 08:42:34 2022
     4    #
     5    # Accessible filesystems, by reference, are maintained under '/dev/disk/'.
     6    # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.
     7    #
     8    # After editing this file, run 'systemctl daemon-reload' to update systemd
     9    # units generated from this file.
    10    #
    11    UUID=7a405199-a904-4290-89a3-72992b4ad489 /                       xfs     defaults        0 0
    12    UUID=6c58d4aa-0c80-4e0b-b372-0a2187033d23 /boot                   ext4    defaults        1 2
    13    UUID=2dd6f870-9df8-47ec-a4fc-b8a01878f4e0 /data                   xfs     defaults        0 0
    14    UUID=27f69466-124d-4124-aab8-85f247d76d57 none                    swap    defaults        0 0
[10:01:14 root@centos8 ~]#

1.1.3  tac 

逆向显示文本内容

[10:03:11 root@centos8 ~]#nl /etc/fstab|tac
    14    UUID=27f69466-124d-4124-aab8-85f247d76d57 none                    swap    defaults        0 0
    13    UUID=2dd6f870-9df8-47ec-a4fc-b8a01878f4e0 /data                   xfs     defaults        0 0
    12    UUID=6c58d4aa-0c80-4e0b-b372-0a2187033d23 /boot                   ext4    defaults        1 2
    11    UUID=7a405199-a904-4290-89a3-72992b4ad489 /                       xfs     defaults        0 0
    10    #
     9    # units generated from this file.
     8    # After editing this file, run 'systemctl daemon-reload' to update systemd
     7    #
     6    # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.
     5    # Accessible filesystems, by reference, are maintained under '/dev/disk/'.
     4    #
     3    # Created by anaconda on Tue Apr  5 08:42:34 2022
     2    # /etc/fstab
     1    #
       
[10:03:56 root@centos8 ~]#

1.1.4  rev

将同一行的内容逆向显示

[10:06:10 root@centos8 /data]#cat f1.txt 
1  2  3  4  5  6  7 

a  b  c  d  e  f  g
[10:06:22 root@centos8 /data]#cat f1.txt |rev
 7  6  5  4  3  2  1

g  f  e  d  c  b  a

1.2  按列抽取文本cut

  cut 命令可以提取文本文件或STDIN数据的指定列

  格式

cut [OPTION]... [FILE]...


常用选项

-d DELIMITER: 指明分隔符,默认tab

-f FILEDS:
         #: 第#个字段,例如:3
         #,#[,#]:离散的多个字段,例如:1,3,6
         #-#:连续的多个字段, 例如:1-6
         混合使用:1-3,7

-c 按字符切割

--output-delimiter=STRING指定输出分隔符

示例:

passwd文件取出第一列用户名

[10:11:47 root@centos8 /data]#cut -d":" -f1 /etc/passwd
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
games
ftp
nobody
dbus
systemd-coredump
systemd-resolve
tss
polkitd
geoclue

[10:17:11 root@centos8 /data]#ifconfig | head -n2| tail -n1 |tr -s " "|cut -d" " -f3
192.168.1.85
[10:17:39 root@centos8 /data]#

1.3 合并多个文件paste

  paste合并多个问价你同行号的列到一行

  格式 

paste [OPTION]... [FILE]...

常用选项

-d  #分隔符:指定分隔符,默认用TAB
-s  #所有行合成一行显示

示例:

[10:28:27 root@centos8 /data]#paste  f1.txt 
1  2  3  4  5  6  7 

a  b  c  d  e  f  g
[10:28:34 root@centos8 /data]#paste -s f1.txt 
1  2  3  4  5  6  7         a  b  c  d  e  f  g
[10:28:43 root@centos8 /data]#
[10:33:32 root@centos8 /data]#paste  f1.txt seq.log 
1  2  3  4  5  6  7     1
    2
a  b  c  d  e  f  g    3
    4
    5
    6
    7
    8
    9
    10
[10:33:55 root@centos8 /data]#paste -d";" f1.txt seq.log 
1  2  3  4  5  6  7 ;1
;2
a  b  c  d  e  f  g;3
;4
;5
;6
;7
;8
;9
;10

2.1分析文本的工具

  2.1.1  wc

  wc命令可用于统计文件的行总数,单词总数,字节总数和字符总数

  可以对文件或STDIN中的数据统计

  常用选项

-l       只计数行数
-w       只计数单词总数
-c       只计数字节总数
-m       只计数字符总数
-L       显示文件中最长行的长度

示例

[10:34:04 root@centos8 /data]#wc /etc/fstab 
 15    78   709   /etc/fstab
行数 单词数 字节数

  2.1.2  sort

  sort:文本排序;把整理过的文本显示在STDOUT,不改变原始文件

  格式:

  

sort [options] file(s)

常用选项

-r          执行反方向(由上至下)整理
-R          随机排序
-n          执行按数字大小整理
-h          人类可读排序,如: 2K 1G
-f          选项忽略(fold)字符串中的字符大小写
-u          选项(独特,unique),合并重复项,即去重
-t c        选项使用c做为字段界定符
-k #        选项按照使用c字符分隔的 # 列来整理能够使用多次
默认是把相同的字符排序在一起

[10:48:15 root@centos8 /data]#cat seq.log 
1
2
3
4
5
6
7
8
9
1
10
11
2
12
8
13
9
14
15
[10:48:21 root@centos8 /data]#cat seq.log |sort
1
1
10
11
12
13
14
15
2
2
3
4
5
6
7
8
8
9
9
[10:49:44 root@centos8 /data]#cat seq.log |sort -u
1
10
11
12
13
14
15
2
3
4
5
6
7
8
9

  2.1.3  uniq

  uniq命令从输入中删除前后相接的重复的行

  格式:

uniq [OPTION]... [FILE]...

常见选项

-c: 显示每行重复出现的次数
-d: 仅显示重复过的行
-u: 仅显示不曾重复的行

uniq常和sort 命令一起配合使用:

  示例:

[10:50:10 root@centos8 /data]#cat seq.log |sort | uniq -c
      2 1
      1 10
      1 11
      1 12
      1 13
      1 14
      1 15
      2 2
      1 3
      1 4
      1 5
      1 6
      1 7
      2 8
      2 9

 作业练习

6、统计出/etc/passwd文件中其默认shell为非/sbin/nologin的用户个数,并将用户都显示出来

 

 

7、查出用户UID最大值的用户名、UID及shell类型

 

 

练习脚本写法

[14:48:51 root@centos8 /data/bashs]#cat user.sh #!/bin/bash cat /etc/passwd |cut -d":" -f3|sort -nr|head -1 > /dev/null maxUID=`grep 65534 /etc/passwd | cut -d":" -f1,3,7` echo "UID最大值的用户:$maxUID" [14:52:53 root@centos8 /data/bashs]#bash user.sh UID最大值的用户:nobody:65534:/sbin/nologin

 

8、统计当前连接本机的每个远程主机IP的连接数,并按从大到小排序

 

posted @ 2022-04-15 17:39  goodbay说拜拜  阅读(119)  评论(0编辑  收藏  举报