[英]how to sort a file in bash whose lines are in a particular format?
all lines in file.txt
are in the following format: file.txt
中的所有行均采用以下格式:
player16:level8|2200 Points
player99:level8|19000 Points
player23:level8|260 Points
how can I sort this file based on points? 如何基于点对文件进行排序? looking for the following output 寻找以下输出
player99:level8|19000 Points
player16:level8|2200 Points
player23:level8|260 Points
Any help would be greatly appreciated. 任何帮助将不胜感激。 Thank you. 谢谢。
sort
is designed for this task sort
是为此任务设计的
sort -t'|' -k2nr file
set the delimiter to |
将定界符设置为|
and sort by the second field numerical reverse order 并按第二个字段数字逆序排序
You've tagged it as perl
so I'll add a perlish answer. 您已将其标记为perl
所以我将添加一个完善的答案。
perl's sort
function lets you specify an arbitary comparison criteria, provided you return 'positive/negative/zero' depending on relative position. 如果根据相对位置返回“正/负/零”,则可以使用perl的sort
函数指定任意比较标准。 By default the <=>
operator does this numerically, and the cmp
operator does that alphabetically. 默认情况下, <=>
运算符以数字方式执行此操作,而cmp
运算符按字母顺序进行操作。
sort
works by setting $a
and $b
to each element of a list in turn, and performing the comparison function for each pair 通过依次将$a
和$b
设置到列表的每个元素,并对每个对执行比较功能来进行sort
So for your scenario - we craft a function that regex matches each line, and extracts the points value: 因此,对于您的情况-我们制作一个正则表达式匹配每行的函数,并提取点值:
sub sort_by_points {
#$a and $b are provided by sort.
#we regex capture one or more numbers that are followed by "Points".
my ($a_points) = $a =~ m/(\d+) Points/;
my ($b_points) = $b =~ m/(\d+) Points/;
#we compare the points - note $b comes first, because we're sorting
#descending.
return $b_points <=> $a_points;
}
And then you use that function by calling sort
with it. 然后,通过调用sort
使用它。
#!/usr/bin/env perl
use strict;
use warnings;
sub sort_by_points {
my ($a_points)= $a =~ m/(\d+) Points/;
my ($b_points) = $b =~ m/(\d+) Points/;
return $b_points <=> $a_points;
}
#reads from the special __DATA__ filehandle.
chomp ( my @list = <DATA> ) ;
my @sorted_list = sort {sort_by_points} @list;
print join "\n", @sorted_list;
__DATA__
player16:level8|2200 Points
player99:level8|19000 Points
player23:level8|260 Points
For your purposes, you can use <>
as your input, because that's the magic file handle - arguments on command line, or data piped through STDIN (might sound weird, but it's the same thing as sed/grep/awk do) 就您的目的而言,您可以使用<>
作为输入,因为这是魔术文件句柄-命令行中的参数,或通过STDIN传递的数据(可能听起来很奇怪,但这与sed / grep / awk一样)
If you ask for Perl solution: 如果您要求Perl解决方案:
perl -F'\|' -e'push@a,[$_,$F[1]]}{print$_->[0]for sort{$b->[1]<=>$a->[1]}@a' file.txt
but using sort
is far simpler 但是使用sort
要简单得多
sort -t'|' -k2nr file.txt
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.