linux下日志分析常用命令总结sort,uniq,head

一、sort命令
      sort命令用来对文本文件进行排序,并将排序结果输出到标准输出,下面通过实例介绍sort各种参数的用法
  1.      sort默认排序为首字符向后,依次按ASCII码值进行比较,最后将他们按升序输出
    $ cat sort.log |sort
    aaccv
    bbkk
    ergefe
    jsdhfh
    qwqw
    rrrr
    rrrr
    wewr
    
     
  2.  sort -u 排序后去除重复结果
    $ cat sort.log |sort -u
    aaccv
    bbkk
    efergferg
    ergefe
    ergfewrg
    ggggg
    jsdhf
    qwqw
    rrrr
    sefhuis
    sssss
    tttt
    wewr
    
    原sort.log
    qwqw
    wewr
    aaccv
    bbkk
    rrrr
    rrrr
    ergefe
    jsdhf
    sefhuis
    sssss
    sssss
    efergferg
    ggggg
    ggggg
    ergfewrg
    tttt
    tttt
     
  3. sort -o 将排序后的内容输出覆盖到当前被排序文件内。(重定向只能新建一个文件)
    yebing.li@QT-liyebing ~/shell_example
    $ sort -u newSort.log  -o newSort.log
    
    yebing.li@QT-liyebing ~/shell_example
    $ more newSort.log
    
    aaccv
    bbkk
    C
    efergferg
    ergefe
    ergfewrg
    ggggg
    h
    jsdhf
    qwqw
    rrrr
    sefhuis
    sssss
    tttt
    wewr
    
     
  4. sort -r ,sort默认的排序是升序,-r改为降序
  5. sort -n 用数字类型来排序,默认是按照ASCII码排序
    yebing.li@QT-liyebing ~/shell_example
    $ cat number.log |sort -rn
    133ee
    6
    5ff
    5
    4r
    3
    3
    3
    2e
    2
    2
    2
    1
    0
    
    
    yebing.li@QT-liyebing ~/shell_example
    $ cat number.log |sort -n
    0
    1
    2
    2
    2
    2e
    3
    3
    3
    4r
    5
    5ff
    6
    133ee
    
     
  6. sort -k -t   ,其中k为指定的列数(从1开始),t列之间的为分隔符,下面的例子为按照分割之后的第二列来排序
     cat number_sep.log |sort -n -k 2 -t :
    werw:1:3
    sdfs:12:4
    weq:66:88
    wdqwfx:99:55
    
    
    number_sep.log的内容:
    sdfs:12:4
    weq:66:88
    wdqwfx:99:55
    werw:1:3
    
    
  7. 其他参数
    -f会将小写字母都转换为大写字母来进行比较,亦即忽略大小写

    -c会检查文件是否已排好序,如果乱序,则输出第一个乱序的行的相关信息,最后返回1

    -C会检查文件是否已排好序,如果乱序,不输出内容,仅返回1

    -M会以月份来排序,比如JAN小于FEB等等

    -b会忽略每一行前面的所有空白部分,从第一个可见字符开始比较。
    参考 http://www.cnblogs.com/51linux/archive/2012/05/23/2515299.html

二、uniq命令
      Note:  'uniq' does not detect repeated lines unless they are adjacent.  You may want to sort the input
first, or use `sort -u' without `uniq'. 
     该命令必须在排序之后再使用。
      
       -c, --count
              prefix lines by the number of occurrences

       -d, --repeated
              only print duplicate lines

       -D, --all-repeated[=delimit-method]
              print all duplicate lines delimit-method={none(default),prepend,separate} Delimiting is done with blank lines

       -f, --skip-fields=N
              avoid comparing the first N fields

       -i, --ignore-case
              ignore differences in case when comparing

       -s, --skip-chars=N
              avoid comparing the first N characters

       -u, --unique
              only print unique lines

       -z, --zero-terminated
              end lines with 0 byte, not newline

       -w, --check-chars=N
              compare no more than N characters in lines
 
三、head命令
        head 用来显示档案的开头至标准输出中 默认head命令打印其相应文件的开头10行。
      
  -c, --bytes=[-]K
              print the first K bytes of each file; with the leading `-', print all but the last K bytes of each file

       -n, --lines=[-]K
              print the first K lines instead of the first 10; with the leading `-', print all but the last K lines of each file

       -q, --quiet, --silent
              never print headers giving file names

       -v, --verbose
              always print headers giving file names
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值