从多个文本文件中提取特定行的数据,以转换为单个 csv 文件
首先,对我糟糕的编码能力表示歉意,但是我花了几个小时阅读论坛并对其进行了破解,所以我非常感谢任何针对以下问题的帮助:
我有 3 个文本文件,我想从中获取文件名、第 3 行数据、第 5 行和第 7 行并将它们弹出到单个 CSV 中,如下所示:
filename1, linedata3, linedata5, linedata7
filename2, linedata3, linedata5, linedata7
filename3, linedata3, linedata5, linedata7
Simples,是吗?不是这样,因为我相当缺乏我的编码“技能”,并且可以在你的帮助下完成。这是我到目前为止所拥有的:
首先是一个批处理文件(go.bat):
@echo off
for /f "skip=2 delims=" %%i in (%1) do >>lines.txt echo %~n1 %%i & goto :EOF
然后是手动命令行条目:
go.bat file1.txt
go.bat file2.txt
go.bat file3.txt
所以,如您所见,我已经为一行文本完成了此操作,但不知道如何附加第 3 行和 5 到输出的末尾。另外,我真正想要的是一个正确的命令行条目,这样我就可以对目录中的所有文本文件执行此操作。我尝试了以下方法,但似乎缺少一些东西:
for %i in (*.*) do go.bat "%i"
身体有帮助吗?
非常感谢! 詹姆斯
First, apologies for my poor coding ability, however I have spent a few hours reading the forums and giving it a crack, so I would really appreciate any help with the following problem:
I have 3 text files, from which I would like to take the filename, 3rd line of data, 5th line, and 7th line and pop them into a single CSV, such as follows:
filename1, linedata3, linedata5, linedata7
filename2, linedata3, linedata5, linedata7
filename3, linedata3, linedata5, linedata7
Simples, eh? not so, for I am rather lacking in my coding "skillz" and could do with your help. Here's what I have so far:
First a batch file (go.bat):
@echo off
for /f "skip=2 delims=" %%i in (%1) do >>lines.txt echo %~n1 %%i & goto :EOF
Then manual command line entries:
go.bat file1.txt
go.bat file2.txt
go.bat file3.txt
So, as you can see, I have done this for one line of text, but don't know how to append lines 3 and 5 onto the end of the output. Also, what I'd really like is a proper command line entry so I can do this for all text files in the directory. I tried the following, but seem to be missing something:
for %i in (*.*) do go.bat "%i"
Any body help?
Thanks muchlies!
James
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如您所说,此批处理文件处理目录中的所有 .txt 文件。
This Batch file process all .txt files in the directory, as you said.
您可以使用另一个结构来读取这些行。
或者用循环(Andriy)
You could read the lines with another construct.
Or with a loop (Andriy)