zoukankan      html  css  js  c++  java
  • 字符处理命令-sort排序,uniq去重,cut剪切文件,tr删除或替换结果集,wc统计

    字符处理命令

    字符处理命令:sort, uniq, cut

    sort命令

    对字符进行排序。

    格式

    sort  需要排序的对象
    
    [root@localhost ~]# cat test.txt 
    s 5
    d 6
    e 1
    h 2
    j 9
    s 5
    h 2
    h 2
    p 11
    o 12
    [root@localhost ~]# sort test.txt 
    d 6
    e 1
    h 2
    h 2
    h 2
    j 9
    o 12
    p 11
    s 5
    s 5
    
    # 注:经过对比得出,sort命令默认情况下对需要排序的对象的第一列进行排序。
    

    参数

    • -k(n) :指定列进行排序,默认情况下是以空格进行分割

      [root@localhost ~]# sort test.txt 
      d 6
      e 1
      h 2
      h 2
      h 2
      j 9
      o 12
      p 11
      s 5
      s 5
      [root@localhost ~]# sort -k2 test.txt 
      e 1
      p 11
      o 12
      h 2
      h 2
      h 2
      s 5
      s 5
      d 6
      j 9
      
      # 注:-k参数只对第一个字符进行排序
      
    • -n : 按照数值的大小进行排序

      [root@localhost ~]# sort -k2 test.txt 
      e 1
      p 11
      o 12
      h 2
      h 2
      h 2
      s 5
      s 5
      d 6
      j 9
      [root@localhost ~]# sort -nk2 test.txt 
      e 1
      h 2
      h 2
      h 2
      s 5
      s 5
      d 6
      j 9
      p 11
      o 12
      
    • -r : 按照倒叙进行排序

      [root@localhost ~]# sort -nk2 test.txt 
      e 1
      h 2
      h 2
      h 2
      s 5
      s 5
      d 6
      j 9
      p 11
      o 12
      [root@localhost ~]# sort -rnk2 test.txt 
      o 12
      p 11
      j 9
      d 6
      s 5
      s 5
      h 2
      h 2
      h 2
      e 1
      
    • -t : 指定分隔符, 后面的排序按照分割符分割的列进行排序

      [root@localhost ~]# sort -t ":" -rnk3 test.txt 
      polkitd:x:999:998:User for polkitd:/:/sbin/nologin
      systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
      abrt:x:173:173::/etc/abrt:/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
      tss:x:59:59:Account used by the trousers package to sandbox the tcsd
      

    习题

    # 复制/etc/passwd到/root/text.txt
    
    cp /etc/passwd /root/text.txt
    
    1、按照倒叙进行排序
    sort -r /root/text.txt
    
    2、按照:进行分割,第三列进行排序
    sort -t ":" -nk3 /root/text.txt
    

    uniq

    对结果集进行去重。

    格式

    uniq [参数]  需要去重的对象
    
    [root@localhost ~]# sort test.txt 
    d 6
    e 1
    h 2
    h 2
    h 2
    j 9
    o 12
    p 11
    s 5
    s 5
    [root@localhost ~]# sort test.txt | uniq 
    d 6
    e 1
    h 2
    j 9
    o 12
    p 11
    s 5
    

    参数

    • -c : 显示重复的次数

      [root@localhost ~]# sort test.txt 
      d 6
      e 1
      h 2
      h 2
      h 2
      j 9
      o 12
      p 11
      s 5
      s 5
      [root@localhost ~]# sort test.txt | uniq 
      d 6
      e 1
      h 2
      j 9
      o 12
      p 11
      s 5
      [root@localhost ~]# sort test.txt | uniq -c
            1 d 6
            1 e 1
            3 h 2
            1 j 9
            1 o 12
            1 p 11
            2 s 5
      # 注: -c参数主要用来显示重复的次数
      
    • -d : 只显示重复的列

      [root@localhost ~]# sort test.txt | uniq -c
            1 d 6
            1 e 1
            3 h 2
            1 j 9
            1 o 12
            1 p 11
            2 s 5
      [root@localhost ~]# sort test.txt | uniq -c -d
            3 h 2
            2 s 5
      
    • -u: 只显示不重复的列

      [root@localhost ~]# sort test.txt | uniq -c
            1 d 6
            1 e 1
            3 h 2
            1 j 9
            1 o 12
            1 p 11
            2 s 5
      [root@localhost ~]# sort test.txt | uniq -c -u
            1 d 6
            1 e 1
            1 j 9
            1 o 12
            1 p 11
      

    习题

    # 复制/etc/passwd到/root/demo.txt
    
    1、按照最后一列(以:进行分割)进行排序,去重
    
    sort /root/demo.txt | uniq 
    
    2、按照最后一列(以:进行分割)进行排序,去重,重复的列
    
    sort /root/demo.txt | uniq -d
    
    

    cut

    剪切文件

    格式:

    cut  [参数]  [操作对象]
    
    [root@localhost ~]# sort -t ":" -nk3  /etc/passwd | cut -d ":" -f1,7
    root:/bin/bash
    bin:/sbin/nologin
    daemon:/sbin/nologin
    adm:/sbin/nologin
    lp:/sbin/nologin
    sync:/bin/sync
    shutdown:/sbin/shutdown
    halt:/sbin/halt
    mail:/sbin/nologin
    operator:/sbin/nologin
    

    参数

    • -d : 指定分割符

      [root@localhost ~]# sort -t ":" -nk3  /etc/passwd | cut -d ":" -f1,7
      root:/bin/bash
      bin:/sbin/nologin
      daemon:/sbin/nologin
      adm:/sbin/nologin
      
    • -f : 指定显示的列

      [root@localhost ~]# sort -t ":" -nk3  /etc/passwd | cut -d ":" -f1,7
      root:/bin/bash
      bin:/sbin/nologin
      daemon:/sbin/nologin
      adm:/sbin/nologin
      

    习题

    [root@qls ~]# echo "root:x:0:0:root:/root:/bin/bash" >test.txt
    [root@qls ~]# cat test.txt
    root:x:0:0:root:/root:/bin/bash
    #取出第七列
    cat test.txt | cut -d ":" -f7
    
    #取出第一列和第七列
    cat test.txt | cut -d ":" -f1,7
    
    #取出第五列到第七列
    cat test.txt | cut -d ":" -f 5-7
    

    tr

    删除或替换结果集

    格式

    tr [参数] [操作对象]
    
    [root@localhost ~]# cat /etc/passwd | tr "root" "ROOT"
    ROOT:x:0:0:ROOT:/ROOT:/bin/bash
    

    参数

    • -d : 删除结果集中的某个字符

      [root@localhost ~]# cat /etc/passwd | tr -d "root"
      :x:0:0::/:/bin/bash
      

    WC

    统计,计算数字

    格式

    wc  [参数]  [操作对象]
    [root@localhost ~]# cat /etc/passwd | wc -l
    21
    [root@localhost ~]# 
    

    参数

    • -l : 统计行数

      [root@localhost ~]# cat /etc/passwd | wc -l
      21
      [root@localhost ~]# 
      
    • -c : 统计字节数

      [root@localhost ~]# cat demo.txt | wc -c
      6
      [root@localhost ~]# 
      
    • -w : 统计单词数

      [root@localhost ~]# cat demo.txt | wc -w
      3
      [root@localhost ~]# 
      
      [root@localhost ~]# cat demo.txt | wc -w
      1
      [root@localhost ~]# vim demo.txt 
      [root@localhost ~]# cat demo.txt | wc -w
      3
      [root@localhost ~]# 
      

    习题

    统计系统文件/etc/services有多少行。
    cat /etc/services | wc -l
    
    统计文件/etc/hosts的行数?你能使用几种方法实现?
    cat /etc/services | wc -l
    wc -l /etc/services
    
    统计文件/etc/services的字节数。你能使用几种方法实现?
    wc -c /etc/services
    
  • 相关阅读:
    转 Hystrix超时实现机制
    Hystrix实现ThreadLocal上下文的传递 转
    MongoDB里做表间关联
    转载:mongodb的两阶段提交实战
    MongoDB中的读写锁
    Mongodb中的 原子性 隔离性
    web服务优化
    MongoDB分片(Sharding)技术
    MongoDB复制集
    consul分布式集群搭建
  • 原文地址:https://www.cnblogs.com/caodan01/p/14513149.html
Copyright © 2011-2022 走看看