CSV 文件处理 Windows 脚本,最好的工具,最简单的方法?

发布于 2024-10-03 08:21:39 字数 398 浏览 0 评论 0原文

在 Windows 平台上,使用尽可能少的工具(更喜欢仅使用内置工具,例如 CMD,但我意识到可能需要 sed 之类的工具);我想创建一个执行如下的脚本。

它应该输入一个 csv 文件,从该文件中提取某些字段,然后输出一个新的且格式独特的输出 csv 文件,其中包含提取的字段。例如。

Input.csv
Cust#, CustFName, CustLName, Address, City, State, Zip, Order#, Qty, Part, Cost, Total,,,,

Output.csv
,,, Qty, City,,,, CustFName,,, Total,,, Zip,,,

csv 文件的记录数量会有所不同。有些将有 3 条记录,另一些将有 10,000 条记录。

On a Windows platform, using the fewest tools possible (prefer to use only built in tools such as CMD but I realize something like sed may be required); I would like create a script that will perform as follows.

It should input a csv file, extract certain fields from that file and then output a new and uniquely formatted output csv file that contains the extracted fields. For example.

Input.csv
Cust#, CustFName, CustLName, Address, City, State, Zip, Order#, Qty, Part, Cost, Total,,,,

Output.csv
,,, Qty, City,,,, CustFName,,, Total,,, Zip,,,

The csv files will vary the number of records. Some will have 3 records and others will have 10,000 records.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

活泼老夫 2024-10-10 08:21:39

您可以使用 VBscript,自 Win2K 以来几乎所有 Windows 系统都可以使用 VBscript。这是一个示例,假设您没有“引用”逗号。

Dim fso, f2, ts, ts2, line, records
Const ForReading = 1, ForWriting = 2, ForAppending = 8

Set fso = CreateObject("Scripting.FileSystemObject")
Set tf1 = fso.OpenTextFile("infile.txt", ForReading, false)
Set tf2 = fso.OpenTextFile("outfile.txt", ForWriting, true)
Do While Not tf1.AtEndOfStream
    line = tf1.ReadLine
    records=Split(line,",")
    ' ... process your records here '
    line = Join(records,",")
    tf2.WriteLine line
Loop
tf1.Close
tf2.Close

从 cmd 行运行此命令,如“cscript yourscript.vbs”。对于“真实”的事情,您可能需要添加一些代码来处理不同数量的记录、逗号是引用记录的一部分的记录等等,但一般方法应该很清楚。

下面是 MS Win2K 脚本指南中的另一个示例:

http://technet.microsoft. com/en-us/library/ee176692.aspx

You can use VBscript, that should be available on almost all Windows systems since Win2K. Here is an example, assuming you don't have "quoted" commas.

Dim fso, f2, ts, ts2, line, records
Const ForReading = 1, ForWriting = 2, ForAppending = 8

Set fso = CreateObject("Scripting.FileSystemObject")
Set tf1 = fso.OpenTextFile("infile.txt", ForReading, false)
Set tf2 = fso.OpenTextFile("outfile.txt", ForWriting, true)
Do While Not tf1.AtEndOfStream
    line = tf1.ReadLine
    records=Split(line,",")
    ' ... process your records here '
    line = Join(records,",")
    tf2.WriteLine line
Loop
tf1.Close
tf2.Close

Run this from the cmd line like "cscript yourscript.vbs". For the "real" thing, you will probably have to add some code to deal with different number of records, records where the comma is part of quoted records and so on, but the general approach should be clear.

Here is another example from the MS Win2K scripting guide:

http://technet.microsoft.com/en-us/library/ee176692.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文