【问题标题】:Bash Cut & Sort IssueBash 剪切和排序问题
【发布时间】:2018-11-30 14:04:12
【问题描述】:

我有一个包含数据的 CSV

$cat content.csv

MD5 : 1d4571a01abfbfe1a653a86109c5587f , Detection : Unknown.Trojan.Generickd , Level : 5, Factor : 5, VT Positives 13
MD5 : 03f44b4a8eb4a3b88d8307452eb5b556 , Detection : Document-Word.Exploit.CVE-2012-0013 , Level : 5, Factor : 5, VT Positives 0
MD5 : 58e9db1ec0fa687ee7c1510504a087c8 , Detection : Document-Powerpoint.Trojan.Vba agent , Level : 5, Factor : 5, VT Positives 4
MD5 : 1d025e72e82199d1524a9249073b338d , Detection : Document-Powerpoint.Trojan.Valyria , Level : 5, Factor : 5, VT Positives 1
MD5 : b3039d8f5d616c77297f0da3d5b444ea , Detection : Win32.Trojan.Dynamer , Level : 5, Factor : 5, VT Positives 36
MD5 : 833ab86e5f3d915dba7eea7e79a9c11e , Detection : Win32.Virus.Sality , Level : 5, Factor : 5, VT Positives 42
MD5 : 4f08e1c23ba22eb3bb1e7a7f2418f187 , Detection : Document-Powerpoint.Trojan.Valyria , Level : 5, Factor : 5, VT Positives 1
MD5 : bed56264438a7da43a98073497c74f73 , Detection : DOS.Virus.Arcv , Level : 5, Factor : 5, VT Positives 31

我想基于分隔符"," 在第 5 个字段(VT 正数)上再次使用所有字段(1 到 5)进行数字 sort

我试过了

$ cut -d"," -f 1,2,3,4,5 kiran  | sort -k 5

MD5 : bed56264438a7da43a98073497c74f73 , Detection : DOS.Virus.Arcv , Level : 5, Factor : 5, VT Positives 31
MD5 : 1d025e72e82199d1524a9249073b338d , Detection : Document-Powerpoint.Trojan.Valyria , Level : 5, Factor : 5, VT Positives 1
MD5 : 4f08e1c23ba22eb3bb1e7a7f2418f187 , Detection : Document-Powerpoint.Trojan.Valyria , Level : 5, Factor : 5, VT Positives 1
MD5 : 58e9db1ec0fa687ee7c1510504a087c8 , Detection : Document-Powerpoint.Trojan.Vba agent , Level : 5, Factor : 5, VT Positives 4
MD5 : 03f44b4a8eb4a3b88d8307452eb5b556 , Detection : Document-Word.Exploit.CVE-2012-0013 , Level : 5, Factor : 5, VT Positives 0
MD5 : 1d4571a01abfbfe1a653a86109c5587f , Detection : Unknown.Trojan.Generickd , Level : 5, Factor : 5, VT Positives 13
MD5 : b3039d8f5d616c77297f0da3d5b444ea , Detection : Win32.Trojan.Dynamer , Level : 5, Factor : 5, VT Positives 36
MD5 : 833ab86e5f3d915dba7eea7e79a9c11e , Detection : Win32.Virus.Sality , Level : 5, Factor : 5, VT Positives 42

sort -t$"," -k 5 -n kiran

MD5 : 03f44b4a8eb4a3b88d8307452eb5b556 , Detection : Document-Word.Exploit.CVE-2012-0013 , Level : 5, Factor : 5, VT Positives 0
MD5 : 1d025e72e82199d1524a9249073b338d , Detection : Document-Powerpoint.Trojan.Valyria , Level : 5, Factor : 5, VT Positives 1
MD5 : 1d4571a01abfbfe1a653a86109c5587f , Detection : Unknown.Trojan.Generickd , Level : 5, Factor : 5, VT Positives 13
MD5 : 4f08e1c23ba22eb3bb1e7a7f2418f187 , Detection : Document-Powerpoint.Trojan.Valyria , Level : 5, Factor : 5, VT Positives 1
MD5 : 58e9db1ec0fa687ee7c1510504a087c8 , Detection : Document-Powerpoint.Trojan.Vba agent , Level : 5, Factor : 5, VT Positives 4
MD5 : 833ab86e5f3d915dba7eea7e79a9c11e , Detection : Win32.Virus.Sality , Level : 5, Factor : 5, VT Positives 42
MD5 : b3039d8f5d616c77297f0da3d5b444ea , Detection : Win32.Trojan.Dynamer , Level : 5, Factor : 5, VT Positives 36
MD5 : bed56264438a7da43a98073497c74f73 , Detection : DOS.Virus.Arcv , Level : 5, Factor : 5, VT Positives 31

尝试了多种组合,它没有按预期工作。请问有什么建议吗?

【问题讨论】:

    标签: bash shell sorting awk cut


    【解决方案1】:

    你可以用这个:

    sort -t"," -k 5.15 -n file
    

    分隔符设置为逗号(字符串前不需要$)。

    执行数字排序的关键是从第 15 个字符开始的第 5 个字段(VT Positives 字符串的长度)。

    正如sort 手册页所述:

    KEYDEF 是 F[.C][OPTS][,F[.C][OPTS]] 表示开始和停止位置,其中 F 是字段编号,C 是字段中的字符位置

    【讨论】:

    • 工作完美。谢谢
    • 不错!绝对是最好的解决方案。
    【解决方案2】:

    由于要排序的key是整行的最后一部分,所以可以把它复制到行的前面,排序,最后再去掉:

    awk '{print $NF, $0}' kiran | sort -g | sed 's/^[0-9]\+//'
    

    【讨论】:

      【解决方案3】:

      删除'VT Positives',排序,再放回去:

      $ sed 's/, VT Positives / , /g' content.csv | sort -t, -n -k 5 | sed 's/\([0-9][0-9]*\)$/VT Positives \1/g'
      

      【讨论】:

        【解决方案4】:

        试试

        sort -k17 -n kiran
        

        输出:

        MD5 : 03f44b4a8eb4a3b88d8307452eb5b556 , Detection : Document-Word.Exploit.CVE-2012-0013 , Level : 5, Factor : 5, VT Positives 0
        MD5 : 58e9db1ec0fa687ee7c1510504a087c8 , Detection : Document-Powerpoint.Trojan.Vba agent , Level : 5, Factor : 5, VT Positives 4
        MD5 : 1d025e72e82199d1524a9249073b338d , Detection : Document-Powerpoint.Trojan.Valyria , Level : 5, Factor : 5, VT Positives 1
        MD5 : 4f08e1c23ba22eb3bb1e7a7f2418f187 , Detection : Document-Powerpoint.Trojan.Valyria , Level : 5, Factor : 5, VT Positives 1
        MD5 : 1d4571a01abfbfe1a653a86109c5587f , Detection : Unknown.Trojan.Generickd , Level : 5, Factor : 5, VT Positives 13
        MD5 : bed56264438a7da43a98073497c74f73 , Detection : DOS.Virus.Arcv , Level : 5, Factor : 5, VT Positives 31
        MD5 : b3039d8f5d616c77297f0da3d5b444ea , Detection : Win32.Trojan.Dynamer , Level : 5, Factor : 5, VT Positives 36
        MD5 : 833ab86e5f3d915dba7eea7e79a9c11e , Detection : Win32.Virus.Sality , Level : 5, Factor : 5, VT Positives 42
        
        • -k # - 此参数指定将用于排序的第一列。 (请注意,此处的列定义为空格分隔的字段;因此使用k17

        【讨论】:

        • 这再次没有正确排序。如果您看到结果,VT 阳性 0 排在第一位,4 排在第二位。
        • 逗号分隔符中的一个字段有空格,所以这种计算字段的方式不起作用。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-03
        • 1970-01-01
        • 2014-12-09
        相关资源
        最近更新 更多