【发布时间】:2015-03-15 06:15:27
【问题描述】:
我需要有关此脚本的帮助。我已经慢慢走到这一步了,但现在需要帮助。
我需要一个脚本,它将文本从节的末尾移动到文件中的某个点,然后删除移动的文本。要移动的文本有标记,位置也有。我需要能够在移动后删除文本。还需要对同一目录下的多个txt文件做。
例如:
Sample Input .txt
A;1;1;####; (#### is the location (1) marker)
B
B
B
====-1234 (==== is the find (1) marker)
A;1;1;####; (#### is the location (2) marker)
B
B
B
====-5678 (==== is the find (2) marker)
After processing
A;1;1;1234;
B
B
B
A;1;1;5678;
B
B
B
文本文件可以有多个这样的分组。需要从上到下对每个分组执行此操作。这是我目前所拥有的,它只是移动文本而不是删除。
$file = "C:\Users\NX07934\Documents\Projects\23045\Docs\SampleData\*.txt"
$old = "\####"
$find = Get-ChildItem $file -recurse| Select-String -pattern "====-*"
$split = $find.ToString().Split("-")
$new = $split[1]
get-childitem "C:\Dir" -recurse -include *.txt |
select -expand fullname |
foreach
{
(Get-Content $_) -replace $old,$new |
Set-Content $_
}
感谢您的所有帮助!
【问题讨论】:
标签: powershell replace foreach