提取多个文本文件中的数据,具体的线路转换为一个CSV文件 [英] extracting specific lines of data from multiple text files, to convert to a single csv file

查看:196
本文介绍了提取多个文本文件中的数据,具体的线路转换为一个CSV文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我可怜的编码能力道歉,但是我已经花了几个小时阅读论坛,并给它一条缝,所以我真的AP preciate有以下问题的任何帮助:

我有3个文本文件,从中我想借文件名,数据,5号​​线的3号线,和第7行,并将其流行到一个CSV,如如下:

 文件名1,linedata3,linedata5,linedata7
文件名2,linedata3,linedata5,linedata7
filename3,linedata3,linedata5,linedata7

Simples,是吗?并非如此,因为我相当缺乏在我的编码的skillz,可能与你的帮助做的。这是我到目前为止有:

首先一个批处理文件(GO.BAT):

 关闭@echo
FOR / F跳过= 2 delims =%% i的(%1)做>> lines.txt回声%〜N1 %% I和GOTO:EOF

然后手动命令行条目:

  GO.BAT FILE1.TXT
GO.BAT FILE2.TXT
GO.BAT file3.txt

所以,你可以看到,我这样做了一行文本,但不知道如何3和第5行追加到输出的结尾。另外,我真正喜欢的是一个正确的命令行输入,所以我可以在目录中的所有文本文件做到这一点。我尝试以下,但似乎失去了一些东西:

 用于%I IN(*。*)做GO.BAT%I

任何机构帮助?

感谢muchlies!
詹姆斯


解决方案

 关闭@echo
SETLOCAL EnableDelayedExpansion
如果存在result.csv德尔result.csv
在%% F(* .TXT)做(
    集I = 0
    在(%% F)/ Fdelims =%%升来做(
        集/ A I + = 1
        集线!我!= %%升
    )
    回声%%楼!3号线!!!LINE5!line7! >> result.csv

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

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

解决方案

@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.

这篇关于提取多个文本文件中的数据,具体的线路转换为一个CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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