添加正则表达式作为新列添加到csv文件[批处理脚本] [英] Add Regex Matches as new columns to the csv file [Batch Scripting]

查看:407
本文介绍了添加正则表达式作为新列添加到csv文件[批处理脚本]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.csv文件,我需要添加正则表达式匹配在每一行作为新列的原始列后,这里是.csv文件的一部分:

I have a .csv file that I need to add regex matches in each line as new columns after the original columns, here is a part of the .csv file:

"Event";"User";"Description"   
"stock_change";"usertest1@gmail.com";"Change Product Teddy-Bear (Shop ID: AR832H0823)"
"stock_update";"usertest2@gmail.com";"Update Product 30142_Pen (Shop ID: GI8759)"

这里是两个Regex模式我想把它们从每一行中提取的结果作为新列(每列一列)

Here is the two Regex Patterns I want to add their extracted results from each row as new columns (one column for each)

(?<=Product\s)\w.*?(?=\s*\(Shop)

(?<=Shop ID:\s)\w.*?(?=\))

数据应该像这样(标题行不重要):

The Result on the data should be Like this (Header Row is not important):

"stock_change";"usertest1@gmail.com";"Change Product Teddy-Bear (Shop ID: AR832H0823)";"Teddy-Bear";"AR832H0823"  
"stock_update";"usertest2@gmail.com";"Update Product 30142_Pen (Shop ID: GI8759)";"30142_Pen";"GI8759"

对不起我是批处理脚本的基础, / p>

Sorry I'm very basic in Batch Scripting, thanks in advance

推荐答案

Windows批处理没有本机regex find / replace实用程序。唯一的regex实用程序是FINDSTR,这是非常有限的和非标准的,它只能打印出匹配搜索的整行 - 它不能打印出匹配的部分。

Windows batch does not have a native regex find/replace utility. The only regex utility is FINDSTR, and that is extremely limited and non-standard, and it can only print out entire lines that match the search - it cannot print out just the matching portion.

您可以使用PowerShell。

You could use PowerShell.

但我会使用 JREPL.BAT - 一种纯基于脚本的实用程序(混合JScript /批处理),可在任何Windows XP机器上运行。它使用ECMA正则表达式,所以没有后备,但它有足够的权力来完成这项任务。

But I would use JREPL.BAT - a purely script based utility (hybrid JScript/batch) that works on any Windows machine from XP onward. It uses ECMA regular expressions, so no look-behind, but it has plenty of power to do the task.

jrepl "Product\s(\S+?)\s*\(Shop ID:\s(.*?)\)\q$" "$&;\q$1\q;\q$2\q" /a /x /f test.csv /o -

c $ c> / a switch丢弃不变的行,这有效地删除标题行。 / o - 选项将使用输出覆盖原始文件。 / x 开关启用扩展转义序列,从而为 c>启用 \q code>。

The /a switch discards unchanged lines, which effectively removes the header line. The /o - option overwrites the original file with the output. The /x switch enables extended escape sequences, thus enabling \q for ".

如果将命令放在批处理脚本中,请使用调用jrepl

Use call jrepl if you put the command in a batch script.

完整的文档可以从命令行通过 jrepl /? jrepl / ?? 用于分页输出。

Full documentation is available from the command line via jrepl /?, or jrepl /?? for paged output.

这篇关于添加正则表达式作为新列添加到csv文件[批处理脚本]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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