Powershell - 查找并替换然后保存
我需要读取 10K+ 个文件,逐行搜索文件,查找单词 SUFFIX
后面的字符串。捕获该字符串后,我需要从文件中删除它的所有痕迹,然后重新保存文件。
通过下面的示例 - 我将捕获 -4541
。然后我会将所有出现的 -4541
替换为 NULL。 一旦我替换了所有出现的情况,我就会保存更改。
这是我的数据:
ABSDOMN VER 1 D SUFFIX -4541
05 ST-CTY-CDE-FMHA-4541
10 ST-CDE-FMHA-4541 9(2)
10 CTY-CDE-FMHA-4541 9(3)
05 NME-CTY-4541 X(20)
05 LST-UPDTE-DTE-4541 9(06)
05 FILLER X
这是一个启动脚本。我可以显示包含单词 SUFFIX 的行,但无法捕获其后面的字符串。在本例中为-4541
。
$CBLFileList = Get-ChildItem -Path "C:\IDMS" -File -Recurse
$regex = "\bSUFFIX\b"
$treat = $false
ForEach($CBLFile in $CBLFileList) {
Write-Host "Processing .... $CBLFile" -foregroundcolor green
Get-content -Path $CBLFile.FullName |
ForEach-Object {
if ($_ -match $regex) {
Write-Host "Found Match - $_" -foregroundcolor green
$treat=$true
}
}
I need to read 10K+ files, search the files line by line, for the string of characters after the word SUFFIX
. Once I capture that string I need to remove all traces of it from the file then re-save the file.
With the example below - I would capture -4541
. Then I would replace all occurrences of -4541
with NULL.
Once I replace all the occurrences I then save the changes.
Here is my Data:
ABSDOMN VER 1 D SUFFIX -4541
05 ST-CTY-CDE-FMHA-4541
10 ST-CDE-FMHA-4541 9(2)
10 CTY-CDE-FMHA-4541 9(3)
05 NME-CTY-4541 X(20)
05 LST-UPDTE-DTE-4541 9(06)
05 FILLER X
Here is a starting script. I can Display the line that has the word SUFFIX but I cannot capture the string after it. In this case -4541
.
$CBLFileList = Get-ChildItem -Path "C:\IDMS" -File -Recurse
$regex = "\bSUFFIX\b"
$treat = $false
ForEach($CBLFile in $CBLFileList) {
Write-Host "Processing .... $CBLFile" -foregroundcolor green
Get-content -Path $CBLFile.FullName |
ForEach-Object {
if ($_ -match $regex) {
Write-Host "Found Match - $_" -foregroundcolor green
$treat=$true
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请尝试以下操作:
Set-Content
的默认编码不同,请将-Encoding
与Set-Content
一起使用来指定所需的编码。根据您的反馈,最终对您有用的正则表达式是
(?<=SUFFIX).*$
(可以简化为(?<=SUFFIX ).+
在这种情况下),即捕获子字符串SUFFIX
后面的内容,而不是仅捕获空格后跟- 和一个或多个数字(
\d+
)。Try the following:
-Encoding
withSet-Content
to specify the desired encoding, if it should be different fromSet-Content
's default.Based on your feedback, the regex that worked for you in the end was
(?<=SUFFIX).*$
(which could be simplified to(?<=SUFFIX).+
in this case), i.e. one that captures whatever follows substringSUFFIX
, instead of only capturing a space followed by a-
and one or more digits (\d+
).