使用 Powershell 根据文本中的其他子字符串更改文本中的多个子字符串

发布于 2025-01-15 21:38:16 字数 744 浏览 3 评论 0原文

我们在 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 技术交流群。

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

发布评论

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

评论(1

没企图 2025-01-22 21:38:16

使用开关 语句从文件中逐行读取 (-File) 并使用 regex(正则表达式)模式匹配 (-Regex):

switch -Regex -File file.txt {
  '^:28C:'          { $saved = $_; continue } # save the line and continue
  '^:60F:C(\d+)EUR' { $saved -replace '00001/00001', $Matches[1]; $_ }
  default           { $_ } # unrelated line, pass through
}
  • <代码>$保存-替换“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):

switch -Regex -File file.txt {
  '^:28C:'          { $saved = $_; continue } # save the line and continue
  '^:60F:C(\d+)EUR' { $saved -replace '00001/00001', $Matches[1]; $_ }
  default           { $_ } # unrelated line, pass through
}
  • $saved -replace '00001/00001', $Matches[1] uses the -replace operator to replace verbatim string 00001/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.

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