如何根据关键词进行排序?我有一个复杂的列表,我想知道我是否可以对它进行排序。这是一个例子:
enter code here Room Name: RM145
,ID,user Name
,300,NOS
,100,NOT
Room Name: RM370
,ID,user Name
,300,NOS
Room Name: RM471
,ID,user Name
,300,POS
,100,NOT
Room Name: RM349
,ID,user Name
,301,NOS
,100,NOT
,500,COS
我的目标是根据“Room Name
”对此列表进行排序。
我有纯文本和CSV文件。我不知道哪一个会更容易排序。有没有人有建议?
答案 0 :(得分:2)
假设排序键是你可以做的房间名称:
#!/usr/bin/env perl
use strict;
use warnings;
my %room_by;
my $roomname;
while (<DATA>) {
chomp;
if ( m/Room Name:\s*(.+)/ ) {
$roomname = $1;
$room_by{$roomname} = ();
}
else {
push @{$room_by{$roomname}}, $_;
}
}
for $roomname (sort keys %room_by) {
print "Room Name: $roomname\n";
for my $content ( @{$room_by{$roomname}} ) {
print "$content\n";
}
}
__DATA__
Room Name: RM145
,ID,user Name
,300,NOS
,100,NOT
Room Name: RM370
,ID,user Name
,300,NOS
Room Name: RM471
,ID,user Name
,300,POS
,100,NOT
Room Name: RM349
,ID,user Name
,301,NOS
,100,NOT
,500,COS
这会在数组哈希中收集房间名称,其中哈希键是房间名称,哈希值是房间的内容。您也可以对哈希值进行排序以对它们进行排序。
答案 1 :(得分:2)
使用 GNU awk 4 :
awk 'END {
PROCINFO["sorted_in"] = "@ind_num_asc"
for (R in r)
print r[R]
}
/Room Name:/ {
rn = substr($NF, 3)
}
{
r[rn] = r[rn] ? r[rn] ORS $0 : $0
}' infile
这个应该适用于较旧的 GNU awk 版本:
WHINY_USERS= awk 'END {
for (R in r)
print r[R]
}
/Room Name:/ {
rn = sprintf("%15s", substr($NF, 3))
}
{
r[rn] = r[rn] ? r[rn] ORS $0 : $0
}' infile
答案 2 :(得分:1)
假设您可以格式化文件,使每条记录都在自己的行上,sort
$ cat ./infile
Room Name: RM145,ID,user Name,300,NOS,100,NOT
Room Name: RM370,ID,user Name,300,NOS
Room Name: RM471,ID,user Name,300,POS,100,NOT
Room Name: RM349,ID,user Name,301,NOS,100,NOT,500,COS
$ sort ./infile
Room Name: RM145,ID,user Name,300,NOS,100,NOT
Room Name: RM349,ID,user Name,301,NOS,100,NOT,500,COS
Room Name: RM370,ID,user Name,300,NOS
Room Name: RM471,ID,user Name,300,POS,100,NOT