使用 Powershell 根据文本中的其他子字符串更改文本中的多个子字符串
我们在 ERP 系统中处理 M940 银行对账单,但银行没有在文件中提供对账单编号(它始终显示 00001/00001),因此我想将对账单日期放在对账单编号应有的位置。每条对账单都有一个以代码 :28C: 开头的对账单编号,以及以 :60F: 开头的行中的对账单日期,如下所示。
:28C:00001/00001
:60F:C220315140379 欧元,24
:28C:00001/00001
:60F:C220316EUR440379,24
如何让 Powershell 将 00001/00001 更改为 :60F: 行后面的日期?在实际情况中,在 :28C: 之前和 :60F: 之后有一些代码行,甚至可能还有其他代码。但是,在 :28C: 行之后总会有一个 :60:-行,其日期。
:28C:220315
:60F:C220315140379 欧元,24
:28C:220316
:60F:C220316EUR440579,58
我已经创建了一些 powershell 脚本,该脚本添加其他必要的子字符串并将其移动到一个目录,具体取决于文件中提到的银行帐户,但这部分脚本不相关对于这个问题,这里不做阐述。
如果有一个链接将我推向正确的方向,我已经很高兴了。
We process M940 bankstatements in our ERP-system but the bank does not provide the statement number in the file (it always states 00001/00001) so I want to put the statement-date where the statement number should be. Each statement has a statement number preceded with code :28C: and a statement date in a line that starts with :60F: such as below.
:28C:00001/00001
:60F:C220315EUR140379,24
:28C:00001/00001
:60F:C220316EUR440379,24
How can I get Powershell to change 00001/00001 into the date coming from the :60F: line right after it? In the real thing there are lines with codes before :28C: and after :60F: and maybe even other codes in between. However, there will always be a :60:-line with the date after a :28C: line.
:28C:220315
:60F:C220315EUR140379,24
:28C:220316
:60F:C220316EUR440579,58
I already have created some powershell script that adds other neccessary substrings and moves it to a directory depending on the bankaccount mentioned in the file but that part of the script is not relevant for this question therefore not stated here.
I would already be very happy with a link that pushes me in the right direction.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
开关
语句从文件中逐行读取 (-File
) 并使用 regex(正则表达式)模式匹配 (-Regex
):<代码>$保存-替换“00001/00001”,$Matches[1] 使用
-replace
运算符来替换逐字字符串00001/00001
与分支条件中的(第一个)捕获组 ((\d+)
) 捕获的内容(即数量,由一个或多个 (+
) 数字 (\d
)),如 自动$Matches
变量变量并输出结果。自动
$_
变量 包含当前的输入行,并且; $_
之后输出它。Use a
switch
statement to read from your file line by line (-File
) and use regex (regular-expression) pattern matching (-Regex
):$saved -replace '00001/00001', $Matches[1]
uses the-replace
operator to replace verbatim string00001/00001
with what the (first) capture group ((\d+)
) in the branch condition captured (i.e. the amount, composed of one or more (+
) digits (\d
)), as reflected in the automatic$Matches
variable variable and outputs the result.The automatic
$_
variable contains the input line at hand, and; $_
outputs it afterwards.