文本文件中的唯一字[重复](Unique words in a text file [duplicate])

编程入门 行业动态 更新时间:2024-10-23 03:22:53
文本文件中的唯一字[重复](Unique words in a text file [duplicate])

这个问题在这里已经有了答案:

解析文本文件中的独特单词 2个答案

我想知道是否有方法在文本文件中查找(并显示)所有唯一的单词(出现一次的单词)? 这可以通过使用命令行来完成吗? 或者我会不得不使用类似python脚本的东西?

This question already has an answer here:

Parsing unique words from a text file 2 answers

I was wondering if there is a way to find (and display) all the unique words (words that appear once) in a text file? Could this be done just using the command line? Or would I have to use something like a python script?

最满意答案

如果你不想编写应用程序,那么我可以想到的最简单的方法就是使用powershell。 看到这个:

https://msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.utility/get-unique

Microsoft提供的示例使用唯一字词列表填充变量:

$A = $(foreach ($line in Get-Content C:\Test1\File1.txt) {$line.tolower().split(" ")}) | sort | Get-Unique

您可能希望使用额外的分隔符来分隔标点符号,例如:

$A = $(foreach ($line in Get-Content C:\test.txt) {$line.tolower().split(" .,?!;:")}) | sort | Get-Unique

把它放在一个扩展名为.ps1的文件中,你可以从命令行运行它。 为了从变量中获取数值,只需在变量的第二行将结果回显给屏幕:

$A

要获得数组中项目的数量,您可以这样做:

$A.count

If you don't want to write an application then the easiest way that I can think to accomplish this is to use powershell. See this:

https://msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.utility/get-unique

The example that Microsoft provides populates a variable with the list of unique words:

$A = $(foreach ($line in Get-Content C:\Test1\File1.txt) {$line.tolower().split(" ")}) | sort | Get-Unique

You may wish you use additional delimiters though to split on punctuation such as this:

$A = $(foreach ($line in Get-Content C:\test.txt) {$line.tolower().split(" .,?!;:")}) | sort | Get-Unique

Place this in a file with the extension .ps1 and you can run it from the command line. In order to get the values out of the variable just a second line with the variable to echo the result to the screen:

$A

To get the count of items in the array you could do this:

$A.count

更多推荐

本文发布于:2023-07-25 06:30:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1257258.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:一字   文本文件   Unique   words   file

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!