zoukankan      html  css  js  c++  java
  • 基础文本处理工具

    文本处理工具:wc、tr、cut、sort、uniq、paste、diff、patch
      wc(word count) 字符统计
      wc [option] [file]...
      -l  统计行数lines
      -w 统计单词数word(以空格、制表符、换行符为分隔符)
      -c 统计字节数character

    演示:
    [root@centos7 ~]# wc -l /etc/passwd
    46 /etc/passwd
    [root@centos7 ~]# wc -w /etc/passwd
    90 /etc/passwd
    [root@centos7 ~]# wc -c /etc/passwd
    2467 /etc/passwd

    [root@centos7 ~]# wc /etc/passwd
    46 90 2467 /etc/passwd

     tr 转换或删除字符,tr '集合1' '集合2',必须使用管道输出作为该命令参数
     转换字符:会把第一个字符集的对应转换为第二个字符集中的对应转换
      [root@localhost ~]# echo "hello word" | tr [a-z] [A-Z]
      HELLO WORD
      -d 删除字符集中出现的字符
        [root@localhost ~]# echo "hello word" | tr -d r
        hello wod
      -s 压缩输入中重复的字符
        [root@localhost ~]# echo "GNU is    not      UNIX. Recursive right ?" | tr -s ' ' (去除连续重复的空白字符)
        GNU is not UNIX. Recursive right ?  
      -c  取"集合1"中字符集的补集,用"集合2"替换"集合1"的补集,要求字符集为ASCII(可以结合-d命令使用)
        反选设定字符,也就是符合"SET1"的部份不做处理,不符合的剩余部份才进行转换

    演示:
    tr 加选项 -t 和不加的区别,如下:
    # 对位替换
    [root@centos7 ~]# echo abcde |tr 'a-c' 'A-Z'
    ABCde
    [root@centos7 ~]# echo abcde |tr 'abc' 'A-Z'
    ABCde
    [root@centos7 ~]# echo abcde |tr 'abc' 'D-Z'
    DEFde
    [root@centos7 ~]# echo abcde |tr 'a-c' 'xyz'
    xyzde

    # 如果SET2对应的位数不及SET1时,会用SET2对应SET1对应的最后一位补全SET1替换位
    [root@centos7 ~]# echo abcde |tr 'abcde' 'xyz'
    xyzzz
    [root@centos7 ~]# echo abcde |tr 'abcd' 'xyz'
    xyzze

    # 将第一字符集对应的字符转化为第二字符集对应的字符。
    [root@centos7 ~]# echo abcde |tr -t 'abcd' 'xyz'
    xyzde

    # 用空格替换回车换行符
    [root@centos7 ~]# ls /root/ |tr ' ' ' '

    #处理字符串“xt.,l 1 jr#!$mn2 c*/fe3 uz4”,只保留其中的数字和空格
    [root@centos7 ~]# echo "xt.,l 1 jr#'[:digit:]'mn2 c*/fe3 uz4" |tr -cd '[:digit:] '
    1 2 3 4

    #将PATH变量显示在独立的一行
    [root@centos7 ~]# echo $PATH
    /usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
    [root@centos7 ~]# echo $PATH |tr ':' ' '
    /usr/lib64/qt-3.3/bin
    /usr/local/sbin
    /usr/local/bin
    /usr/sbin
    /usr/bin
    /root/bin

     cut 根据指定的分隔符切片,显示出需要显示的片
      -d字符 指定分隔符
      -f数字 指定要显示的字段
      -c数字 显示指定数字的字符
        单个数字:一个字段
        多个离散字段:逗号分隔
        多个连续字段:-分隔
      --output-delimiter=STRING  指定输出结果的分隔符

    演示:
    [root@centos7 ~]# tail -5 /etc/passwd
    memcached:x:990:985:Memcached daemon:/run/memcached:/sbin/nologin
    zabbix:x:989:984:Zabbix Monitoring System:/var/lib/zabbix:/sbin/nologin
    hacluster:x:189:189:cluster user:/home/hacluster:/sbin/nologin
    tomcat:x:91:91:Apache Tomcat:/usr/share/tomcat:/bin/nologin
    arclinux:x:1002:1002::/home/arclinux:/bin/bash

    # 以":"为分隔符,取出第1列,第3列,第7列
    [root@centos7 ~]# tail -5 /etc/passwd |cut -d: -f1,3,7
    memcached:990:/sbin/nologin
    zabbix:989:/sbin/nologinx
    hacluster:189:/sbin/nologin
    tomcat:91:/bin/nologin
    arclinux:1002:/bin/bash

    # 指定输出分隔符
    [root@centos7 ~]# tail -5 /etc/passwd |cut -d: -f1,3,7 --output-delimiter=--
    memcached--990--/sbin/nologin
    zabbix--989--/sbin/nologin
    hacluster--189--/sbin/nologin
    tomcat--91--/bin/nologin
    arclinux--1002--/bin/bash
    [root@centos7 ~]# tail -5 /etc/passwd |cut -d: -f1,3,7 |tr ':' '--'
    memcached-990-/sbin/nologin
    zabbix-989-/sbin/nologin
    hacluster-189-/sbin/nologin
    tomcat-91-/bin/nologin
    arclinux-1002-/bin/bash

    # 按字符进行切割
    [root@centos7 ~]# head -5 /etc/passwd |cut -c1-5
    root:
    bin:x
    daemo
    adm:x
    lp:x:

     sort 文本排序,默认按ASSIC码字符进行比较排序
      格式 sort[options] FILE
      -f 忽略大小写
      -r 逆序
      -n 对数字进行排序
      -t 指定分隔符
      -k数字 指定分隔后,按指定字段进行排序(与-t结合使用)
      -u 重复的行,只显示一行

    演示:
    [root@centos7 ~]# head -5 /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

    # -t 指定分隔符,-k 指明字段,-n 按数值大小进行排序,-r 逆序排列
    [root@centos7 ~]# head -5 /etc/passwd |sort -t: -k3 -n
    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

    [root@centos7 ~]# head -5 /etc/passwd |sort -t: -k3 -nr
    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

    sort -u
    [root@centos7 ~]# cut -d: -f7 /etc/passwd |sort
    /bin/bash
    /bin/bash
    /bin/bash
    /bin/bash
    /bin/bash
    /bin/nologin
    /bin/sync
    /sbin/halt
    /sbin/nologin
    /sbin/nologin
    /sbin/nologin
    /sbin/nologin
    /sbin/nologin
    /sbin/nologin
    /sbin/nologin
    /sbin/nologin
    /sbin/nologin
    /sbin/nologin
    /sbin/nologin
    /sbin/nologin
    /sbin/nologin
    /sbin/nologin
    /sbin/nologin
    /sbin/nologin
    /sbin/nologin
    /sbin/nologin
    /sbin/nologin
    /sbin/nologin
    /sbin/nologin
    /sbin/nologin
    /sbin/nologin
    /sbin/nologin
    /sbin/nologin
    /sbin/nologin
    /sbin/nologin
    /sbin/nologin
    /sbin/nologin
    /sbin/nologin
    /sbin/nologin
    /sbin/nologin
    /sbin/nologin
    /sbin/nologin
    /sbin/nologin
    /sbin/nologin
    /sbin/nologin
    /sbin/nologin
    /sbin/shutdown

    # 除去连续重复的行,只保留一次
    [root@centos7 ~]# cut -d: -f7 /etc/passwd |sort -u
    /bin/bash
    /bin/nologin
    /bin/sync
    /sbin/halt
    /sbin/nologin
    /sbin/shutdown

    # 实现的功能同sort -u
    [root@centos7 ~]# cut -d: -f7 /etc/passwd |sort |uniq
    /bin/bash
    /bin/nologin
    /bin/sync
    /sbin/halt
    /sbin/nologin
    /sbin/shutdown

    [root@centos7 ~]# cut -d: -f7 /etc/passwd |sort -u |wc -l
    6

      uniq 移除重复的行(默认不加参数重复的行显示一次)
      -c 统计每一行出现的次数
      -d 仅显示出现最少两次的行(显示重复的行一次)
      -u 仅显示不重复的行(重复的行直接不显示)

      注意:
      连续且完全相同的行为重复
      常和 sort 命令一起配合使用(sort -u和sort|uniq相同)
    演示:
    # 把文件中所有的非字母的用回车换行符“ ”替换并压缩,-c 为取[:alpha:]的补集,-s 为压缩
    [root@centos7 ~]# cat /etc/init.d/functions |tr -cs '[:alpha:]' ' '
    eq
    success
    base
    startup
    failure
    base
    startup
    A
    function
    to
    stop
    a
    program
    killproc
    local
    RC
    killlevel
    base

    # 将转换出来的结果排序,默认是按字符顺序排序
    [root@centos7 ~]# cat /etc/init.d/functions |tr -cs '[:alpha:]' ' ' |sort
    is
    is
    is
    is
    it
    it
    it
    it
    it
    it
    its
    kill
    kill
    kill
    kill
    Kill
    KILL
    KILL
    killlevel
    killlevel
    killlevel
    killlevel
    killlevel
    killlevel
    killlevel
    killlevel
    killproc
    killproc
    killproc
    l
    lang
    LANGSH
    LANGSH
    LANGSH
    let
    lets
    level
    level

    # uniq -c 去重并显示每行重复的次数
    [root@centos7 ~]# cat /etc/init.d/functions |tr -cs '[:alpha:]' ' ' |sort |uniq -c
    9 user
    1 using
    2 usleep
    4 usr
    1 uU
    2 v
    13 var
    6 verbose
    4 via
    5 warning
    5 WARNING
    1 whether
    3 while
    1 width
    13 x
    1 xcondrestart
    1 xforce
    1 xreload
    1 xrestart
    1 xstart
    1 xstop
    1 xtry
    3 yes
    2 yY
    24 z

    # 按重复的次数进行数值大小排序
    [root@centos7 ~]# cat /etc/init.d/functions |tr -cs '[:alpha:]' ' ' |sort |uniq -c |sort -n
    1
    1 aA
    1 abnormally
    1 strstr
    1 SubState
    1 succeeded
    1 sys
    1 t
    1 TEXTDOMAIN
    1 true
    1 txt
    1 ulimit
    1 unknown
    1 unless
    1 unset
    1 used
    1 Useful
    1 using
    1 uU
    1 whether
    1 width
    1 xcondrestart
    2 startup
    2 stop
    2 style
    2 sure
    2 SYSTEMCTL
    2 TERM
    2 they
    2 This
    2 tT
    2 umask
    2 up
    2 update
    2 usleep
    3 killproc
    3 LANGSH
    3 lib
    3 NICELEVEL
    3 only
    3 options
    3 or
    3 proc
    3 restart
    3 ret
    3 sbin
    3 serial
    3 show
    3 something
    4 line
    4 nicelevel
    4 passed
    4 plymouth
    4 reload
    4 shutdown
    4 use
    4 usr
    5 Test
    5 that
    5 warning
    5 WARNING
    6 c
    6 continue
    6 i
    6 init
    6 it
    6 MOVE
    7 nice
    7 not
    7 prog
    7 success
    7 sysconfig
    7 Usage
    8 cgroup
    8 COL
    8 delay
    8 killlevel
    8 lock
    8 pidof
    8 running
    9 do
    9 done
    9 else
    9 failure
    9 pidfile
    10 rc
    10 s
    10 the
    10 to
    11 null
    12 f
    12 pids
    13 color
    13 o
    13 program
    13 run
    13 var
    13 x
    14 bin
    14 dev
    14 shift
    15 systemctl
    16 etc
    16 SETCOLOR
    16 sysctl
    17 is
    19 p
    20 a
    20 local
    21 BOOTUP
    21 RC
    23 d
    23 in
    24 z
    29 base
    33 n
    45 fi
    45 return
    47 then
    51 echo
    54 file
    55 if
    67 pid

      paste 合并两个文件同行号的列到一行
      -d 指定分隔符,默认用TAB
      -s 所有行合成一行显示
    演示:
    [root@centos7 ~]# cat /testdir/f1
    aaaaaaaa
    bbbbbbbb
    cccccccc
    dddddddd
    [root@centos7 ~]# cat /testdir/f2
    anaconda-ks.cfg
    Desktop
    Discuz
    Documents

    [root@centos7 ~]# paste /testdir/f1 /testdir/f2
    aaaaaaaa anaconda-ks.cfg
    bbbbbbbb Desktop
    cccccccc Discuz
    dddddddd Documents

    [root@centos7 ~]# paste -s /testdir/f1 /testdir/f2
    aaaaaaaa bbbbbbbb cccccccc dddddddd
    anaconda-ks.cfg Desktop Discuz Documents

      diff 比较两个文件之间的区别
      diff命令的输出被保存在一种叫做"补丁"的文件中
      使用-u选项来输出"统一的(unified)"diff格式文件,最适用于补丁文件
      patch 复制对文件的改变
      patch命令复制在其它文件中进行的改变(要谨慎使用)
      用-b选项来自动备份改变了的文件

    演示:
    [root@centos7 ~]# cat /testdir/f1
    aaaaaaaa
    bbbbbbbb
    cccccccc
    dddddddd
    eeeeeeee
    ffffffff

    [root@centos7 ~]# cat /testdir/f2
    aaaaaaaa
    bbbbbbbb
    cccccccc
    ddddhhdd
    eeeeeeee
    fffffddd

    # diff命令注明第4行和第6行不同
    [root@centos7 ~]# diff /testdir/f1 /testdir/f2
    4c4
    < dddddddd
    ---
    > ddddhhdd
    6c6
    < ffffffff
    ---
    > fffffddd

    # 备份恢复
    [root@centos7 ~]# patch -b /testdir/f1 /testdir/diff.log
    patching file /testdir/f1

    [root@centos7 ~]# ls /testdir/
    diff.log f1 f1.orig

    [root@centos7 ~]# cat /testdir/f1 # 为原来的f2
    aaaaaaaa
    bbbbbbbb
    cccccccc
    ddddhhdd
    eeeeeeee
    fffffddd
    [root@centos7 ~]# cat /testdir/f1.orig # 为原来的f1
    aaaaaaaa
    bbbbbbbb
    cccccccc
    dddddddd
    eeeeeeee
    ffffffff

    练习题:
    1、找出ifconfig命令结果中本机的所有IPv4地址
    [root@centos7 ~]# ifconfig
    eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
    inet 192.168.1.113 netmask 255.255.255.0 broadcast 192.168.1.255
    inet6 fe80::20c:29ff:fed6:e460 prefixlen 64 scopeid 0x20<link>
    ether 00:0c:29:d6:e4:60 txqueuelen 1000 (Ethernet)
    RX packets 9063 bytes 6222807 (5.9 MiB)
    RX errors 0 dropped 0 overruns 0 frame 0
    TX packets 3753 bytes 602301 (588.1 KiB)
    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 0 (Local Loopback)
    RX packets 4 bytes 340 (340.0 B)
    RX errors 0 dropped 0 overruns 0 frame 0
    TX packets 4 bytes 340 (340.0 B)
    TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

    [root@centos7 ~]# ifconfig |head -2
    eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
    inet 192.168.1.113 netmask 255.255.255.0 broadcast 192.168.1.255

    [root@centos7 ~]# ifconfig |head -2 |tail -1
    inet 192.168.1.113 netmask 255.255.255.0 broadcast 192.168.1.255

    [root@centos7 ~]# ifconfig |head -2 |tail -1 |tr ' ' ':'
    ::::::::inet:192.168.1.113::netmask:255.255.255.0::broadcast:192.168.1.255

    [root@centos7 ~]# ifconfig |head -2 |tail -1 |tr -s ' ' ':'
    :inet:192.168.1.113:netmask:255.255.255.0:broadcast:192.168.1.255

    [root@centos7 ~]# ifconfig |head -2 |tail -1 |tr -s ' ' ':' |cut -d: -f3
    192.168.1.113

    2、查出分区空间使用率的最大百分比值
    [root@centos7 ~]# df
    文件系统 1K-块 已用 可用 已用% 挂载点
    /dev/sda2 41922560 533688 41388872 2% /
    devtmpfs 486140 0 486140 0% /dev
    tmpfs 500664 0 500664 0% /dev/shm
    tmpfs 500664 6836 493828 2% /run
    tmpfs 500664 0 500664 0% /sys/fs/cgroup
    /dev/sda3 20961280 3027236 17934044 15% /usr
    /dev/sda1 496300 140512 355788 29% /boot
    tmpfs 100136 0 100136 0% /run/user/0

    [root@centos7 ~]# df |tr -s ' ' ':'
    文件系统:1K-块:已用:可用:已用%:挂载点
    /dev/sda2:41922560:533688:41388872:2%:/
    devtmpfs:486140:0:486140:0%:/dev
    tmpfs:500664:0:500664:0%:/dev/shm
    tmpfs:500664:6836:493828:2%:/run
    tmpfs:500664:0:500664:0%:/sys/fs/cgroup
    /dev/sda3:20961280:3027236:17934044:15%:/usr
    /dev/sda1:496300:140512:355788:29%:/boot
    tmpfs:100136:0:100136:0%:/run/user/0

    [root@centos7 ~]# df |tr -s ' ' ':' |cut -d: -f5
    已用%
    2%
    0%
    0%
    2%
    0%
    15%
    29%
    0%

    [root@centos7 ~]# df |tr -s ' ' ':' |cut -d: -f5 |tr -d '%'
    已用
    2
    0
    0
    2
    0
    15
    29
    0

    3、查出用户UID最大值的用户名、UID及shell类型
    [root@centos7 ~]# getent 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
    avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
    systemd-bus-proxy:x:999:997:systemd Bus Proxy:/:/sbin/nologin
    systemd-network:x:998:996:systemd Network Management:/:/sbin/nologin
    dbus:x:81:81:System message bus:/:/sbin/nologin
    polkitd:x:997:995:User for polkitd:/:/sbin/nologin
    abrt:x:173:173::/etc/abrt:/sbin/nologin
    colord:x:996:994:User for colord:/var/lib/colord:/sbin/nologin
    libstoragemgmt:x:995:992:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin
    setroubleshoot:x:994:991::/var/lib/setroubleshoot:/sbin/nologin
    rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
    rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin
    chrony:x:993:990::/var/lib/chrony:/sbin/nologin
    tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
    geoclue:x:992:989:User for geoclue:/var/lib/geoclue:/sbin/nologin
    usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
    mysql:x:27:27:MariaDB Server:/var/lib/mysql:/sbin/nologin
    pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
    gdm:x:42:42::/var/lib/gdm:/sbin/nologin
    rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
    nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
    postfix:x:89:89::/var/spool/postfix:/sbin/nologin
    sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
    ntp:x:38:38::/etc/ntp:/sbin/nologin
    tcpdump:x:72:72::/:/sbin/nologin
    mageedu:x:1000:1000:mageedu:/home/mageedu:/bin/bash
    apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
    nginx:x:991:986:nginx user:/var/cache/nginx:/sbin/nologin
    centos:x:1001:1001:centos,110101,110110,11223:/home/centos:/bin/bash
    memcached:x:990:985:Memcached daemon:/run/memcached:/sbin/nologin
    zabbix:x:989:984:Zabbix Monitoring System:/var/lib/zabbix:/sbin/nologin
    hacluster:x:189:189:cluster user:/home/hacluster:/sbin/nologin
    tomcat:x:91:91:Apache Tomcat:/usr/share/tomcat:/bin/nologin
    arclinux:x:1002:1002::/home/arclinux:/bin/bash

    # 取出要求的字段列
    [root@centos7 ~]# getent passwd |cut -d: -f1,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
    avahi-autoipd:170:/sbin/nologin
    systemd-bus-proxy:999:/sbin/nologin
    systemd-network:998:/sbin/nologin
    dbus:81:/sbin/nologin
    polkitd:997:/sbin/nologin
    abrt:173:/sbin/nologin
    colord:996:/sbin/nologin
    libstoragemgmt:995:/sbin/nologin
    setroubleshoot:994:/sbin/nologin
    rpc:32:/sbin/nologin
    rtkit:172:/sbin/nologin
    chrony:993:/sbin/nologin
    tss:59:/sbin/nologin
    geoclue:992:/sbin/nologin
    usbmuxd:113:/sbin/nologin
    mysql:27:/sbin/nologin
    pulse:171:/sbin/nologin
    gdm:42:/sbin/nologin
    rpcuser:29:/sbin/nologin
    nfsnobody:65534:/sbin/nologin
    postfix:89:/sbin/nologin
    sshd:74:/sbin/nologin
    ntp:38:/sbin/nologin
    tcpdump:72:/sbin/nologin
    mageedu:1000:/bin/bash
    apache:48:/sbin/nologin
    nginx:991:/sbin/nologin
    centos:1001:/bin/bash
    memcached:990:/sbin/nologin
    zabbix:989:/sbin/nologin
    hacluster:189:/sbin/nologin
    tomcat:91:/bin/nologin
    arclinux:1002:/bin/bash

    # 以":"为分隔符,第二个字段,进行数值大小排序
    [root@centos7 ~]# getent passwd |cut -d: -f1,3,7 |sort -t: -k2n
    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
    mysql:27:/sbin/nologin
    rpcuser:29:/sbin/nologin
    rpc:32:/sbin/nologin
    ntp:38:/sbin/nologin
    gdm:42:/sbin/nologin
    apache:48:/sbin/nologin
    tss:59:/sbin/nologin
    tcpdump:72:/sbin/nologin
    sshd:74:/sbin/nologin
    dbus:81:/sbin/nologin
    postfix:89:/sbin/nologin
    tomcat:91:/bin/nologin
    nobody:99:/sbin/nologin
    usbmuxd:113:/sbin/nologin
    avahi-autoipd:170:/sbin/nologin
    pulse:171:/sbin/nologin
    rtkit:172:/sbin/nologin
    abrt:173:/sbin/nologin
    hacluster:189:/sbin/nologin
    zabbix:989:/sbin/nologin
    memcached:990:/sbin/nologin
    nginx:991:/sbin/nologin
    geoclue:992:/sbin/nologin
    chrony:993:/sbin/nologin
    setroubleshoot:994:/sbin/nologin
    libstoragemgmt:995:/sbin/nologin
    colord:996:/sbin/nologin
    polkitd:997:/sbin/nologin
    systemd-network:998:/sbin/nologin
    systemd-bus-proxy:999:/sbin/nologin
    mageedu:1000:/bin/bash
    centos:1001:/bin/bash
    arclinux:1002:/bin/bash
    nfsnobody:65534:/sbin/nologin

    # 取最后一行
    [root@centos7 ~]# getent passwd |cut -d: -f1,3,7 |sort -t: -k2n |tail -1
    nfsnobody:65534:/sbin/nologin

    4、查出/tmp的权限,以数字方式显示
    [root@CentOS6 ~]# stat /tmp
    File: `/tmp'
    Size: 4096 Blocks: 8 IO Block: 4096 directory
    Device: fd00h/64768d Inode: 1048577 Links: 6
    Access: (1777/drwxrwxrwt) Uid: ( 0/ root) Gid: ( 0/ root)
    Access: 2017-02-12 09:19:45.983655283 +0800
    Modify: 2017-02-12 09:37:35.312654459 +0800
    Change: 2017-02-12 09:37:35.312654459 +0800

    [root@CentOS6 ~]# stat /tmp |head -4 |tail -1
    Access: (1777/drwxrwxrwt) Uid: ( 0/ root) Gid: ( 0/ root)

    [root@CentOS6 ~]# stat /tmp |head -4 |tail -1 |cut -d: -f2
    (1777/drwxrwxrwt) Uid

    # 删除除了数字外的其他字符(即其补集)
    注意:后面的" "需要添加上,换行符也会被当做除数字外的补集被删除,导致不能换行
    [root@CentOS6 ~]# stat /tmp |head -4 |tail -1 |cut -d: -f2 |tr -cd '[:digit:] '
    1777 

  • 相关阅读:
    LeetCode——Generate Parentheses
    LeetCode——Best Time to Buy and Sell Stock IV
    LeetCode——Best Time to Buy and Sell Stock III
    LeetCode——Best Time to Buy and Sell Stock
    LeetCode——Find Minimum in Rotated Sorted Array
    Mahout实现基于用户的协同过滤算法
    使用Java对文件进行解压缩
    LeetCode——Convert Sorted Array to Binary Search Tree
    LeetCode——Missing Number
    LeetCode——Integer to Roman
  • 原文地址:https://www.cnblogs.com/Link-Luck/p/9849855.html
Copyright © 2011-2022 走看看