zoukankan      html  css  js  c++  java
  • shell之sort和uniq 及wc 的使用

    文本排序:sort
          -n:数值排序
          -r: 降序
          -t: 字段分隔符
          -k: 以哪个字段为关键字进行排序
          -u: 排序后相同的行只显示一次
          -f: 排序时忽略字符大小写
    ##########################################################################
    DESCRIPTION
     -n, --numeric-sort
                  compare according to string numerical value
           -r, --reverse
                  reverse the result of comparisons
           -t, --field-separator=SEP
                  use SEP instead of non-blank to blank transition
           -k, --key=POS1[,POS2]
                  start a key at POS1, end it at POS2 (origin 1)
           -u, --unique
                  with -c, check for strict ordering; without -c, output only the first of an equal run
           -f, --ignore-case
                  fold lower case to upper case characters
    例子:/etc/passwd 的第三例数字从小到大排列
    [root@zdx30 ~]# 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
    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
    news:x:9:13:news:/etc/news:
    uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
    operator:x:11:0:operator:/root:/sbin/nologin
    games:x:12:100:games:/usr/games:/sbin/nologin
    gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
    ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
    nscd:x:28:28:NSCD Daemon:/:/sbin/nologin
    rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
    rpc:x:32:32:Portmapper RPC user:/:/sbin/nologin
    ntp:x:38:38::/etc/ntp:/sbin/nologin
    xfs:x:43:43:X Font Server:/etc/X11/fs:/sbin/nologin
    mailnull:x:47:47::/var/spool/mqueue:/sbin/nologin
    smmsp:x:51:51::/var/spool/mqueue:/sbin/nologin
    haldaemon:x:68:68:HAL daemon:/:/sbin/nologin
    vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin
    avahi:x:70:70:Avahi daemon:/:/sbin/nologin
    sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
    pcap:x:77:77::/var/arpwatch:/sbin/nologin
    dbus:x:81:81:System message bus:/:/sbin/nologin
    nobody:x:99:99:Nobody:/:/sbin/nologin
     
    ##########################uniq#######################
    uniq:
          -c: 显示文件中行重复的次数
          -d: 只显示重复的行(相邻的行)
          -D: 把所有重复的行都显示出来(相邻的行)
     
    例子:
    [root@data-1-3 scripts]# cat 1.txt
    456
    456
    123
    11
    34
    5678
    456
    [root@data-1-3 scripts]# uniq -c 1.txt
          2 456
          1 123
          1 11
          1 34
          1 5678
          1 456
    [root@data-1-3 scripts]# uniq -c 1.txt
          2 456
          1 123
          1 11
          1 34
          1 5678
          1 456
    [root@data-1-3 scripts]# uniq -d 1.txt
    456
    [root@data-1-3 scripts]# uniq -D 1.txt 
    456
    456
    [root@data-1-3 scripts]# sort  1.txt |uniq -d
    456
    [root@data-1-3 scripts]# sort  1.txt |uniq -D
    456
    456
    456
    [root@data-1-3 scripts]# sort  1.txt |uniq -c
          1 11
          1 123
          1 34
          3 456
          1 5678
    ###########################WC使用##################
    文本统计:wc (word count)
          -l:多少行
          -w:多少单词
          -c:多少字符
          -L:最长的一行包含了多少个字符
    [root@data-1-3 scripts]# wc /etc/fstab
     15    78      779   /etc/fstab
     行数  单词数   字节
    [root@data-1-3 scripts]# wc -l /etc/fstab
    15 /etc/fstab
    [root@data-1-3 scripts]# wc -w /etc/fstab 
    78 /etc/fstab
    [root@data-1-3 scripts]# wc -c /etc/fstab 
    779 /etc/fstab
    [root@data-1-3 scripts]# wc -m /etc/fstab 
    779 /etc/fstab
     
  • 相关阅读:
    hdu 1251(字典树)(3种方法)
    HDU 2203(KMP算法)
    九度oj 题目1335:闯迷宫
    poj3894 bfs+记录路径
    状压dp--P2704
    状压dp--洛谷P2622
    动态规划--牛客多校number
    完全背包
    01背包--hdu
    莫比乌斯反演模板--Gym 101982B
  • 原文地址:https://www.cnblogs.com/shanhua-fu/p/6902597.html
Copyright © 2011-2022 走看看