PowerShell - 从文本文件中查找并复制 URL,然后复制到另一个由管道分隔的文本文件

PowerShell - Find and Copy URL from text file and copy to another text file separated by Pipe

我需要一个 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共 "Replace: "

/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
    }
}