zoukankan      html  css  js  c++  java
  • sort如何按指定的列排序

    <1>[root@localhost company]# cat test
    06d7            145             41925425
    06e1            127             41925425
    06e3            230             41925425
    06ff            3847            41925425
    070d            3408            41925425
    0715            4               41925425
    0726            134             41925425
    我想按第二列值进行排序,从大到小的顺序
    [root@localhost company]# sort -n -k2  test     或   sort -g -k2 test             //在这没有区别,在别的地是有区别的
    0715            4               41925425
    06e1            127             41925425
    0726            134             41925425
    06d7            145             41925425
    06e3            230             41925425
    070d            3408            41925425
    06ff            3847            41925425
    0715            4               41925425
    06e1            127             41925425
    0726            134             41925425
    06d7            145             41925425
    06e3            230             41925425
    070d            3408            41925425
    06ff            3847            41925425

    -n, --numeric-sort          compare according to string numerical value

    -g, --general-numeric-sort  compare according to general numerical value

    -k, --key=POS1[,POS2]     start a key at POS1, end it at POS2 (origin 1)

    -t, --field-separator=SEP  use SEP instead of non-blank to blank transition

    linux的bash shell中有个函数叫做sort,其中有个flag是-t,这个flag的作用是指定用户定义的字符作为分隔符(bash shell 默认用' '和' '作为分隔符),但是当指定的字符为' '时,用法有些特别了,如下:

    cat test.txt|sort -k 1 -t $' ' > sort.txt

    cat test.txt|sort -k 2 -t ' ' > sort.txt

    注意到区别了吗?如果指定的分隔符是' ',我们需要用$来使它表示本来的意思,否则sort程序无法识别' ',而对于一般的制定字符,比如' '和':'就不用了。当然,用$' '和$":"也是可以的。

    <2>sort -g -k2 -t':' -r nat.cfg          //按照以":"为分割的最后一列反向排序

    20012 192.168.5.249:20012
    30062 192.168.5.249:20011
    30042 192.168.5.228:20010
    20005 192.168.5.232:20005
    20000 192.168.5.161:20000
    60251 192.168.5.251:22
    60250 192.168.5.250:22
    60249 192.168.5.249:22
    60248 192.168.5.248:22
    60247 192.168.5.247:22

  • 相关阅读:
    Working with WordprocessingML documents (Open XML SDK)
    How to Choose the Best Way to Pass Multiple Models in ASP.NET MVC
    Azure:Manage anonymous read access to containers and blobs
    Convert HTML to PDF with New Plugin
    location.replace() keeps the history under control
    On the nightmare that is JSON Dates. Plus, JSON.NET and ASP.NET Web API
    HTTP Modules versus ASP.NET MVC Action Filters
    解读ASP.NET 5 & MVC6系列(6):Middleware详解
    Content Negotiation in ASP.NET Web API
    Action Results in Web API 2
  • 原文地址:https://www.cnblogs.com/itcomputer/p/4623944.html
Copyright © 2011-2022 走看看