首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Bash剪切和排序问题

Bash剪切和排序问题
EN

Stack Overflow用户
提问于 2018-06-21 09:30:59
回答 4查看 226关注 0票数 1

我有一个带有数据的CSV

代码语言:javascript
复制
$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

我想在第五个字段上做数值sort ( VT阳性),基于分隔符",",再加上所有字段(1到5)。

我试过了

代码语言:javascript
复制
$ 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

尝试了多个组合,它不像预期的那样工作。有什么建议吗?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2018-06-21 09:45:54

你可以用这个:

代码语言:javascript
复制
sort -t"," -k 5.15 -n file

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

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

正如sort手册页面中所说的那样:

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

票数 4
EN

Stack Overflow用户

发布于 2018-06-21 09:47:57

由于要排序的关键是整行的最后一部分,所以您可以在行前面复制它,对其进行排序,最后再删除它:

代码语言:javascript
复制
awk '{print $NF, $0}' kiran | sort -g | sed 's/^[0-9]\+//'
票数 1
EN

Stack Overflow用户

发布于 2018-06-21 09:51:48

删除'VT阳性‘,排序,再放回去:

代码语言:javascript
复制
$ sed 's/, VT Positives / , /g' content.csv | sort -t, -n -k 5 | sed 's/\([0-9][0-9]*\)$/VT Positives \1/g'
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50965146

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档