我需要一个Powershell脚本,该脚本读取给定文件中出现的关键字"Match:"和"Replace:",并将其前面的行复制到另一个文本文件中的新行,以管道分隔和结束。下面是示例。
输入
20/01/2016 00:00:18 Some Lines of Text here
20/01/2016 00:00:18 Some Lines of Text here
20/01/2016 00:00:18 Match: /test/OLD/Myfolders/Folders/Folder1/
20/01/2016 00:00:19 Replace: /test2/NEW/currentfiles/
20/01/2016 00:00:19 Some Lines of Text here
20/01/2016 00:00:20 Some Lines of Text here
20/01/2016 00:00:20 Some Lines of Text here
20/01/2016 00:00:20 Match: /match/2015/pages/
20/01/2016 00:00:20 Replace: /replace/2016/pages/
20/01/2016 00:00:21 Some Lines of Text here
输出
/test/OLD/Myfolders/Folders/Folder1/|/test2/NEW/currentfiles/|
/match/2015/pages/|/replace/2016/pages/|
所以每次找到关键字"Match:"时,它前面的 URL 都会复制到新文本文件中的新行,后跟管道和"替换:"前面的 URL
/Matching URL/|/Replacing URL/|
/Matching URL/|/Replacing URL/|
你可以试试这个:
Get-Content input.txt | ForEach-Object {
if ($_.tostring().Contains("Match")) {
$i = $_.tostring().IndexOf("Match")
$url = $_.ToString().Substring($i+7).Trim() + "|"
} elseif ($_.tostring().Contains("Replace")) {
$i = $_.ToString().IndexOf("Replace")
$url = $url + $_.ToString().Substring($i+9).Trim() + "|"| Out-File ouput.txt -Append
$url = $null
}
}