PowerShell:如何按列对文本文件进行排序? [英] PowerShell: How do I sort a text file by column?

查看:355
本文介绍了PowerShell:如何按列对文本文件进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在PowerShell中对文本文件进行排序.文本文件如下所示:

I want to sort a text file in PowerShell. The text file looks like this:

name1 4
name2 2.3
name3 6.7
name4 5.1

我想像这样输出该文件:

I want to output that file like this:

name3 6.7
name4 5.1
name1 4
name2 2.3

如您所见,它是按与名称相关的数字降序排列的.我该怎么做?

As you can see, it is ordered descending by the number associated to the name.How do I do that?

推荐答案

您可以按表达式排序,拆分每行(空格分隔符),将最后一项转换为system.double并对其进行排序:

You can sort by an expression, split each line (space delimiter), cast the last item to system.double and sort on it:

Get-Content .\file.txt | Sort-Object { [double]$_.split()[-1] } -Descending

这篇关于PowerShell:如何按列对文本文件进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆