Windows批处理脚本:收集文件中字符串的唯一出现 [英] Windows Batch Scripting: Collect unique occurences of a string in a file

查看:83
本文介绍了Windows批处理脚本:收集文件中字符串的唯一出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件,其中逗号前的第一个字符串是某种标识符.这是一个示例:

I have a file, in which the first string before the comma is some kind of identifier. Here is a sample:

A, bla, bla...  
B, bla, bla...  
A, bla, bla...  
C, bla, bla...

我需要解析一个文件来收集此字符串的所有唯一出现次数.因此,理想情况下,经过处理后,我将拥有某种数组[A, B, C].问题是批处理脚本不支持正式数组.我知道有一些解决方法,但是我签出的方法看起来很难看.

I need to parse a file to collect all unique occurences of this string. So, ideally, after processing I would have some kind of array [A, B, C]. The problem is that officially arrays are not supported in batch scripting. I know there are some workarounds, but the ones I checked out looked quite ugly.

到目前为止,我的情况是这样的:

What I have so far, is something like this:

FOR /F "tokens=1 delims=, " %%i in (%FILE%) do (
    echo %%i
)

这将产生输出:

A
B
A
C

如何消除重复出现的字符串?实现这一目标的优雅方法是什么?

How do I eliminate the duplicate occurences of a string? What would be the elegant way to achieve this?

请分享您对如何解决此问题的想法.

Please, share your thoughts, on how this problem could be solved.

推荐答案

FOR /F "tokens=1 delims=," %%i in (FILE) do ( find "%%i" "%temp%\u" >nul 2>&1 || <nul set/p=%%i,>> "%temp%\u")
type "%temp%\u"

此操作是一行一行地获取文件,在第一个逗号之前获取所有内容,然后将其传递给do. 循环的do部分尝试在包含唯一字符串的文件中查找字符串.如果是,则返回true,并且从不评估第二部分.如果找不到,则将字符串后跟逗号写入文件中.

what this does, is take the file line by line, grab everything before the first comma, and pass it in to the do. the do section of the loop attempts to find the string in a file containing the unique strings. if it does, than it returns true, and the second part is never evaluated. if it does not find it, than it writes the string followed by a comma to the file.

这篇关于Windows批处理脚本:收集文件中字符串的唯一出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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