Bash 剪切和排序问题
Bash Cut & Sort Issue
我有一个包含数据的 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 Positives ) 上再次使用所有字段 ( 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
尝试了多种组合,没有达到预期的效果。有什么建议吗?
你可以使用这个:
sort -t"," -k 5.15 -n file
分隔符设置为逗号(字符串前不需要 $
)。
进行数值排序的关键是从第15个字符开始的第5个字段(VT Positives
字符串的长度)。
如 sort
手册页所述:
KEYDEF is F[.C][OPTS][,F[.C][OPTS]] for start and stop position, where F is a field number and C a character position in the field
尝试
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
)
由于要排序的key是整行的最后一部分,你可以把它复制到该行的前面,排序,最后再剥离:
awk '{print $NF, [=10=]}' kiran | sort -g | sed 's/^[0-9]\+//'
删除'VT Positives',排序,再放回去:
$ sed 's/, VT Positives / , /g' content.csv | sort -t, -n -k 5 | sed 's/\([0-9][0-9]*\)$/VT Positives /g'
我有一个包含数据的 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 Positives ) 上再次使用所有字段 ( 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
尝试了多种组合,没有达到预期的效果。有什么建议吗?
你可以使用这个:
sort -t"," -k 5.15 -n file
分隔符设置为逗号(字符串前不需要 $
)。
进行数值排序的关键是从第15个字符开始的第5个字段(VT Positives
字符串的长度)。
如 sort
手册页所述:
KEYDEF is F[.C][OPTS][,F[.C][OPTS]] for start and stop position, where F is a field number and C a character position in the field
尝试
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
)
由于要排序的key是整行的最后一部分,你可以把它复制到该行的前面,排序,最后再剥离:
awk '{print $NF, [=10=]}' kiran | sort -g | sed 's/^[0-9]\+//'
删除'VT Positives',排序,再放回去:
$ sed 's/, VT Positives / , /g' content.csv | sort -t, -n -k 5 | sed 's/\([0-9][0-9]*\)$/VT Positives /g'