从多个文本文件中提取特定行的数据,以转换为单个 csv 文件

发布于 2024-12-11 18:09:28 字数 816 浏览 0 评论 0原文

首先,对我糟糕的编码能力表示歉意,但是我花了几个小时阅读论坛并对其进行了破解,所以我非常感谢任何针对以下问题的帮助:

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

坠似风落 2024-12-18 18:09:28
@echo off
setlocal EnableDelayedExpansion
if exist result.csv del result.csv
for %%f in (*.txt) do (
    set i=0
    for /F "delims=" %%l in (%%f) do (
        set /A i+=1
        set line!i!=%%l
    )
    echo %%f, !line3!, !line5!, !line7! >> result.csv
)

正如您所说,此批处理文件处理目录中的所有 .txt 文件。

@echo off
setlocal EnableDelayedExpansion
if exist result.csv del result.csv
for %%f in (*.txt) do (
    set i=0
    for /F "delims=" %%l in (%%f) do (
        set /A i+=1
        set line!i!=%%l
    )
    echo %%f, !line3!, !line5!, !line7! >> result.csv
)

This Batch file process all .txt files in the directory, as you said.

一梦等七年七年为一梦 2024-12-18 18:09:28

您可以使用另一个结构来读取这些行。

setlocal EnableDelayedExpansion
< %1 (
Set /p line1=

Set /p line2=
Set /p line3=
Set /p line4=
Set /p line5=
Set /p line6=
Set /p line7=
)
Echo %1,!line3!,!line5!,!line7!

或者用循环(Andriy)

Setlocal EnableDelayedExpansion
<%1 (
  For /L %%n in (1 1 7) do (
    Set /p line%%n=
  )
)
Echo %1,!line3!,!line5!,!line7!

You could read the lines with another construct.

setlocal EnableDelayedExpansion
< %1 (
Set /p line1=

Set /p line2=
Set /p line3=
Set /p line4=
Set /p line5=
Set /p line6=
Set /p line7=
)
Echo %1,!line3!,!line5!,!line7!

Or with a loop (Andriy)

Setlocal EnableDelayedExpansion
<%1 (
  For /L %%n in (1 1 7) do (
    Set /p line%%n=
  )
)
Echo %1,!line3!,!line5!,!line7!
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文