将文件中的文本批量写入本地变量错误 [英] Batch write text from file into local variable error

查看:72
本文介绍了将文件中的文本批量写入本地变量错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的情况如下:

我有一个拥有700.000+行的巨大文件.我必须使用它,我现在将此文件命名为trc.txt

I have a huge file with 700.000+ lines. I have to work with this, I name this file now trc.txt

此文件每一行的结构如下:

The structure of each line of this file is like so:

20958 191014 07:43:57.08 CCComRPC DCMSGCFW_E PID:00000864.00001F40 Data:23
< PREP_FIXED::Process 0

我有第二个文件,我称它为classID.txt,有300行.每行具有以下结构:

I have a seconde file, I call it classID.txt with 300 lines. Each line have the following structure:

ID_Key;ClassName
720;ComEFM
721;CCComRPC
725;ComSSL
730;WOSA-CRD
731;WOSA-PIN

现在的目的是检查我的trc.txt多久可以找到一个特定的Class.

The aim is now to check my trc.txt how often a specific Class can be found.

不同的可能的类名称存储在classID.txt中,并且该名称可以在trc.txt内部每一行中从左数第四个元素中找到.

The different possible class names are stored in the classID.txt and the name can be found in the fourth element from the left in each line inside the trc.txt.

我现在的过程是将不同的可能的ClassName保存在列表变量中.为此,我使用了此for循环(由此帖子)

My procedure right now was to save the different possible ClassNames inside a list-variable. For this I used this for-loop (oriented by this post)

set trcClasses=
for /f "tokens=2 delims=;"  %%i in (classID.txt) do set trcClasses=!trcClasses!,%%i

这似乎很好.

现在要满足我的目标,我想逐行遍历我的大列表trc.txt并每次检查trcClasses的一个元素是否出现.如果是这样,为了计数,我实现了一个简单的计数器,然后将其加1,为此,我正在使用以下代码:

Now to cope with my aim, I thought to iterate through my big-list trc.txt line by line and check each time if one element of the trcClasses occur. If this is so, to count, I implement a simple counter which then increments by one and for that I am using the following code:

for /f "tokens=4 delims= "  %%t in (trc.txt) do (
set "dataRow=%%~t"
set "break="
    for %%l in (%trcClasses%) do if not defined break (
        if not "!dataRow:%%l=!"=="!dataRow!" (
            set /a kumSum%%l+=1
            set "break=1"
        )
    )
)

然后我用这个返回值:

for%%l in (%trcClasses%) do (
    if (!kumSum%%l! NEQ 0) echo %%l !kumSum%%l!
)

第一个问题:控制台在classID.txt中的某些项目上有问题.我收到这样的消息:

First problem: Console have problems with some items in the classID.txt. I receive something like this:

Error: Division durch Null.
Missing operator

我认为这是由classID.txt中的某些名称引起的,例如WOSA-PTRTCP/IP

In my opinion this is caused by some of the names inside classID.txt like WOSA-PTR or TCP/IP

更大的问题:运行代码大约需要花费时间. 12分钟!

The bigger problem: Running the code takes approx. 12 minutes!

任何建议将不胜感激.

推荐答案

您没有指定所需的输出格式,所以我不得不猜测.用for /f循环读取每一行的速度很慢,因此处理300行的文件要比700000行的文件好(现代计算机的缓存系统会很有帮助).

You didn't specify your desired output format, so I had to guess. Reading each line with a for /f loop is slow, so it's better to process the 300 line file than the 700000 line file (the caching system of modern computers will help a lot).

@echo off
setlocal
for /f "skip=1 tokens=2 delims=;" %%a in (classID.txt) do (
  <nul set /p "=%%a;"
  <trc.txt find /c "%%a"
)

我添加了skip=1来跳过classID.txt中的标题行.
缺点是,您必须读取300次大文件,但这仍然比逐行处理它要快(我很感谢有关这两种方法速度比较的一些反馈)

I added skip=1 to skip the header line in classID.txt.
The downside is, you have to read the big file ~300 times, but that still should be faster than processing it line by line (I'd appreciate some feedback about speed comparison of the two methods)

输出示例文件:

ComEFM;0
CCComRPC;1
ComSSL;0
WOSA-CRD;0
WOSA-PIN;0

PS:我假设您想将结果保存在文件中.不要一行一行地写它(for循环内的>> out.txt.这会花一些时间,因为必须打开文件,读取直到文件末尾",然后为每行追加并关闭.一次完整的循环:

PS: I assume you want to have the result in a file. Don't write it line by line (>> out.txt inside the for loop. That takes ages because the file has to be opened, read until "end of file", appended and closed again for each single line. Instead redirect the whole loop at once:

(for /f "skip=1 tokens=2 delims=;" %%a in (classID.txt) do (
  <nul set /p "=%%a;"
  <trc.txt find /c "%%a"
))>out.txt

这篇关于将文件中的文本批量写入本地变量错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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