0

I need a Powershell script which reads a given file for any occurrence of the keywords "Match: " and "Replace: " and copy the line in front of it to a new line in another text file separated and ended by pipe. Example below.

Input

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

Output

/test/OLD/Myfolders/Folders/Folder1/|/test2/NEW/currentfiles/|
/match/2015/pages/|/replace/2016/pages/|

so every time the keyword "Match: " is found the URL in front of it is copied to a new line in the new text file followed by pipe and the URL in front of "Replace: "

/Matching URL/|/Replacing URL/|
/Matching URL/|/Replacing URL/|
Ped
  • 13
  • 6
  • I don't have time to write up a full answer, but `([regex]"[\d\/: ]+Match: (?.*?)\s*[\r\n]+[\d\/: ]+Replace: (?.*?)\s*[\r\n]+").matches(GCI $File -Raw)).groups|?{$_.value -match "[\d\/: ]+Match: (?.*?)\s*[\r\n]+[\d\/: ]+Replace: (?.*?)\s*[\r\n]+"}|%{$Matches["Match"],$Matches["Replace"] -join '|'}|Set-Content $FileOut` should do what you need. [RegEx Link](https://regex101.com/r/cB3vQ2/1) – TheMadTechnician Feb 05 '16 at 01:38
  • srsly, you should try to do it yourself before posting here – Bum Feb 05 '16 at 02:18
  • thanks giving me something to start off with, I wish I could at least write simple codes like this, anyways this is something thats been pushed on to me to do, I am really dumb when it comes to writing code. if anyone is able to help me get this working I would be grateful – Ped Feb 05 '16 at 03:30

1 Answers1

0

you can try this:

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
    }
}
Bum
  • 89
  • 1
  • 6