将文本文件中的行批量提取到一个 CSV
所以我有一个非常简单的问题,但我有点弱智,所以需要一些帮助!
我需要一个执行以下操作的命令行批处理文件:
我有 5,000 个文本文件,它们都具有相同的结构(即每行上的相同字段),但数据不同。我想从第 3 行和第 5 行批量提取所有文件的数据,并将其放入一个简单的 CSV 中,该 CSV 还标记了文件名。
例如,文本文件(标记为 1.txt 到 5000.txt)如下所示:
[data on line 1]
[data on line 2]
[data on line 3]
[data on line 4]
[data on line 5]
[data on line 6]
我想要的 CSV 输出如下所示:
[Filename 1.txt],[Data from line 3],[Data from line 5]
[Filename 2.txt],[Data from line 3],[Data from line 5]
依此类推...
[Filename 5000.txt],[Data from line 3],[Data from line 5]
有什么想法吗?
So I have a very simple question, but I am a little retarded and so need some help!
I need a command-line batch file that does the following:
I have 5,000 text files, that all have the same structure (i.e. the same fields on each line), but different data. I would like to extract data from lines 3 and 5 in batch for all the files, and put this into one simple CSV, that also labels the filename.
For example, the text files (labled 1.txt through to 5000.txt) look like this:
[data on line 1]
[data on line 2]
[data on line 3]
[data on line 4]
[data on line 5]
[data on line 6]
And the desired output in CSV I am after is something like this:
[Filename 1.txt],[Data from line 3],[Data from line 5]
[Filename 2.txt],[Data from line 3],[Data from line 5]
and so forth...
[Filename 5000.txt],[Data from line 3],[Data from line 5]
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有一个名为 LogParser 的免费 Microsoft 工具,可让您使用 SQL 从与查询匹配的文本行中的指定列返回数据,但您希望返回指定的行,而不是列。
MS-DOS 中曾经有一个名为 EDLIN 的行编辑器。它仍然可以在 Windows 7 的命令窗口中使用。可以在批处理文件中使用它来提取所需的行。
祝你好运 !
There is a free Microsoft tool called LogParser that lets you use SQL to return data from specified columns in lines of text matching a query, but you want to return specified lines, not columns.
There used to be a line-editor called EDLIN in MS-DOS. It's still available in a command window in Windows 7. It may be possible to use it in a batch file to extract the lines you want.
Good luck !
或者也许在 VBA 中使用 MS Excel 中的文件系统对象?
如果您不喜欢编程,您可以研究一下键盘宏记录器。通常,您可以点击“录制”并手动为一个文件执行此操作,Alt+Tab;向下箭头到下一个文件并点击“停止”。然后将宏分配给组合键。反复敲击组合键。一次会处理一个文件。对于 5,000 个文件,您可能会发现 5,000 次击键比在不熟悉的环境中学习一种奇怪的编程语言更容易。
我认为您的文本文件中没有行号?即第 1 行以“1”开头,等等......这将使生活变得更容易(参见上面的 LogParser)。可能有什么东西可以将行号添加到现有文件中?
Or maybe use the File System Object from within MS Excel in VBA ?
If programming isn't your thing, you could look into keyboard macro recorders. Typically you hit 'record' and do it manually for one file, Alt+Tab; DownArrow to the next file and hit 'stop'. Then assign the macro to a key combination. Repeatedly hitting the key combination. will do one file at a time. For 5,000 files, you may find 5,000 keystrokes is easier than learning a strange programming language in an unfamiliar environment.
I don't suppose you have line numbers in the text files ? ie Line 1 begins with "1," etc ... that would make life much easier (see LogParser above). There may be something out there that adds line numbers to an existing file ?