多列批处理.txt [英] Batch .txt processing with multiple columns

查看:90
本文介绍了多列批处理.txt的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小问题,我想使用以下批处理脚本来编辑数据.

I have a little problem, I would like to edit data with the following batch script.

@echo off

set "txt=C:\Users\Desktop\test-batch\input.txt"
set "temp=C:\Users\Desktop\test-batch\output.txt"

for /f "tokens=1-43 delims=; " %%a in (%txt%) do echo %%a;%%ac;%%ad;%%ae;%%af;%%ag;%%ah;%%ai;%%aj;%%ak;%%al;%%am;%%an;%%ao;%%ap;%%aq; > %temp%

input.txt:

input.txt:

1;2;2;1;1;1;1;1;1;1;;;;;;;;;;;;;;;;1;;;1-Trackingnummer;2-Trackingnummer;3-Trackingnummer;4-Trackingnummer;5-Trackingnummer;6-Trackingnummer;7-Trackingnummer;8-Trackingnummer;9-Trackingnummer;10-Trackingnummer;11-Trackingnummer;12-Trackingnummer;13-Trackingnummer;14-Trackingnummer;15-Trackingnummer;

output.txt (列1和列29-43):

output.txt (column 1 and columns 29-43):

1;1-Trackingnummer;2-Trackingnummer;3-Trackingnummer;4-Trackingnummer;5-Trackingnummer;6-Trackingnummer;7-Trackingnummer;8-Trackingnummer;9-Trackingnummer;10-Trackingnummer;11-Trackingnummer;12-Trackingnummer;13-Trackingnummer;14-Trackingnummer;15-Trackingnummer;

有人可以告诉我问题出在哪里以及为什么不起作用吗?

Can someone tell me where the problem is and why it doesn't work?

推荐答案

下面是一个潜在的解决方案,它不使用与for /f循环相同的方式使用tokensdelimiters.这样的想法是输出semicolon分隔行(根据您的示例) 的标记129-43.

What follows is a potential solution which doesn't use tokens and delimiters in the same way as your for /f loop. The idea of this is to output tokens 1 and 29-43 of your semicolon delimited lines, (as per your example).

在测试文件之前,请不要忘记纠正23行中的文件路径.

Please don't forget to correct the file paths in lines 2 and 3 before testing it.

@Echo Off&SetLocal EnableExtensions DisableDelayedExpansion
Set "source=C:\Users\Desktop\test-batch\input.txt"
Set "target=C:\Users\Desktop\test-batch\output.txt"

If Exist "%source%" (For %%G In ("%source%")Do If "%%~aG" GEq "d" GoTo :EOF)Else GoTo :EOF
For %%G In ("%target%\..")Do If "%%~aG" Lss "d" GoTo :EOF

For /F "Delims==" %%G In ('Set f[ 2^>NUL')Do Set "%%G="

(For /F Delims^=^ EOL^= %%G In ('Type "%source%"')Do Call :Sub "%%G") 1> "%target%"
GoTo :EOF

:Sub
SetLocal EnableDelayedExpansion
If "%~1" == "" Exit /B
Set "line=%~1"
Set "i=1"
Set "f[!i!]=%line:;="&Set /A i+=1&Set "f[!i!]=%"
Set "out=!f[1]!;"
For /L %%I In (29,1,43)Do Set "out=!out!!f[%%I]!;" 
Echo(%out%
EndLocal
Exit /B

5行和6行只是检查源和目标是否有效.如果源文件不是现有文件,或者目标文件目录不存在,则会关闭脚本.

Line 5 and 6 are just checks that your source and target are valid. If the source is not an existing file, or target file directory does not exist, the script is closed.

8行确保我们没有名称以f[开头的预定义变量.

Line 8 ensures that we have no pre-defined variables with names beginning with f[.

10行从源文件中读取,并将每行传递到:Sub标签,该标签执行令牌解析.

Line 10 reads from the source file and passes each line to the :Sub label which performs the token parsing.

:Sub标签中的代码使用定界符分割每个令牌,将每个令牌设置为一个自变量(f[#],其中#是令牌编号).然后将所需的变量加入到保持变量(%out%)中,该变量最终被echo修改为%target%.

The code in the :Sub label splits each token using the delimiter, setting each to an independent variable, (f[#], where # is the token number). The required variables are then joined in a holding variable, (%out%), which is finally echoed to %target%.

在您的情况下,您的大多数令牌都是完整范围,因此我能够使用令牌1(第19行)和所有范围传播%out% 29..45在行20上.如果令牌更改,则可以将1920行替换为Set "out=",例如For %%I In (1 29 32 34 35 37 39 43)Do Set "out=!out!!f[%%I]!;".

In your case the majority of your tokens were a complete range, so I was able to propagate %out% with token 1, (line 19), and all of the range 29..45 on line 20. If your tokens change then you may replace lines 19 and 20 to Set "out=", and e.g. For %%I In (1 29 32 34 35 37 39 43)Do Set "out=!out!!f[%%I]!;" respectively.

请注意,编写此想法是为了能够执行您在问题中提出的任务.它具有行字符长度限制,并且令牌数限制为允许的环境大小.

这篇关于多列批处理.txt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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