美文网首页
文本查看以及处理工具

文本查看以及处理工具

作者: SRE1 | 来源:发表于2019-04-02 19:48 被阅读0次

    wc:word count

    格式:wc [OPTION]... [FILE]...

    [root@centos7 ~]# wc /etc/fstab
     12  60 595 /etc/fstab
    12行 60个单词 595个字节
    

    -l: lines

    [root@centos7 ~]# wc -l /etc/fstab
    12 /etc/fstab
    

    -w:words

    [root@centos7 ~]# wc -w /etc/fstab
    60 /etc/fstab
    

    -c: bytes

    [root@centos7 ~]# wc -c /etc/fstab
    595 /etc/fstab          
    

    cut:

    格式:cut OPTION... [FILE]...

    -d CHAR:以指定的字符为分隔符

    [root@centos7 ~]# cut -d: -f1,7 /etc/shadow
    root:
    bin:
    daemon:
    adm:
    lp:
    sync:
    shutdown:
    

    -f FIELDS:挑选出的字段

    字符名称 含义
    # 常指定的单个字段
    #-# 连续的多个字段
    #,# 离散的多个字段
    [root@centos7 ~]# cut -d: -f1,3-5,7 /etc/passwd
    root:0:0:root:/bin/bash
    bin:1:1:bin:/sbin/nologin
    daemon:2:2:daemon:/sbin/nologin
    

    练习:取出/etc/rc.d/init.d/functions的行号

    [root@centos7 ~]# wc -l /etc/rc.d/init.d/functions 
    712 /etc/rc.d/init.d/functions
    [root@centos7 ~]# wc -l /etc/rc.d/init.d/functions | cut -d' ' -f1
    712
    

    sort:

    格式:sort [OPTION]... [FILE]...

    [root@node1 ~]# sort /etc/passwd
    adm:x:3:4:adm:/var/adm:/sbin/nologin
    bin:x:1:1:bin:/bin:/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
    

    -n:基于数值大小而非字符进行排序

    [root@node1 ~]# cut -d: -f3 /etc/passwd
    5
    6
    7
    8
    11
    12
    14
    99
    192
    81
    999
    74
    89
    1000
    [root@node1 ~]# cut -d: -f3 /etc/passwd | sort -n
    5
    6
    7
    8
    11
    12
    14
    74
    81
    89
    99
    192
    999
    1000
    

    -t CHAR:指定分隔符
    -k #:用于排序比较的字段

    [root@centos6 ~]# sort -t: -k3 -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
    

    -r:逆序排序
    -f:忽略字符大小写
    -u:重复的行只保留一份
    重复行:连续且相同;

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

    uniq:报告或移除重复的行

    格式:uniq [OPTION]... [INPUT [OUTPUT]]
    -c:显示每行的重复次数

    [root@node1 ~]# cut -d: -f7 /etc/passwd | sort | uniq -c
      2 /bin/bash
      1 /bin/sync
      1 /sbin/halt
     14 /sbin/nologin
      1 /sbin/shutdown
    

    -u:仅显示未曾重复过的行

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

    -d:仅显示重复过的的行

    [root@node1 ~]# cut -d: -f7 /etc/passwd | sort | uniq -d
    /bin/bash
    /sbin/nologin
    

    diff:compare files line by line

    格式:diff [OPTION]... FILES

    [root@centos7 ~]# diff fstab fstab.new
    2c2
    < #
    ---
    > # comment
    

    diff /PATH/TO/OLDFILE /PATH/TO/NEWFILE > /PATH/TO/PATCH_FILE
    -u:使用unfied机制,即显示要修改的行的上下文,默认为3行

    [root@centos7 ~]# diff -u fstab fstab.new
    --- fstab   2019-04-03 17:38:50.823506229 +0800
    +++ fstab.new   2019-04-03 17:30:49.208821687 +0800
    @@ -1,5 +1,5 @@
    
    -#
    +# comment
     # /etc/fstab
     # Created by anaconda on Sun Mar 24 14:30:18 2019
     #
    

    patch:向文件打补丁

    patch [OPTIONS] -i /PATH/TO/PATCH_FILE /PATH/TO/OLDFILE
    patch /PATH/TO/OLDFILE < /PATH/TO/PATCH_FILE

    [root@centos7 ~]# diff fstab fstab.new > fstab.patch
    [root@centos7 ~]# patch -i fstab.patch fstab
    patching file fstab
    [root@centos7 ~]# diff fstab fstab.new 
    [root@centos7 ~]# 
    

    -R:还原补丁

    [root@centos7 ~]# patch -R -i fstab.patch fstab
    patching file fstab
    

    练习:取出ifconfig ens33命令结果中的ip地址;

    [root@centos7 ~]# ifconfig ens33 | grep netmask | tr -s ' ' | cut -d ' ' -f3
    192.168.0.107

    相关文章

      网友评论

          本文标题:文本查看以及处理工具

          本文链接:https://www.haomeiwen.com/subject/ocqnbqtx.html