Powershell: Copy a string/word from a bottom line to a top line

Suzana Eree 811 Reputation points
2021-05-15T05:25:43.487+00:00

hello. I have several html files in C:\Folder1 And I want to copy a word/string below in a line above. For example I want to copy TWO-2 from the second line into the first line, replacing the word ONE-1

html code ... several lines

text <p> BEBE ONE-1 NERO <div> text...

 other  html code ... several lines

 text <table> OANA TWO-2 BOGDAN <tr> text

 again html code ... several lines

MUST BECOME

html code ... several lines

text <p> BEBE TWO-2 NERO <div> text...

 other  html code ... several lines

 text <table> OANA TWO-2 BOGDAN <tr> text

 again html code ... several lines

can this be done with PowerShell?

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

Answer accepted by question author
  1. Andreas Baumgarten 129.4K Reputation points MVP Volunteer Moderator
    2021-05-15T21:44:21.843+00:00

    Hi @Suzana Eree ,

    please try this (last try for today):

    $sourcedir = "./Junk/1"  
    $resultsdir = "./Junk/2"  
    Get-ChildItem -Path $sourcedir -Filter *.html | ForEach-Object {  
        $content = Get-Content -Path $_.FullName -Raw  
        $replaceValue = ((Select-String -Pattern "(BEBE).+(NERO)" -InputObject $content).Matches.Value).Split(" ")[1]    
        $findReplacement = Select-String -Pattern "(OANA).+(BOGDAN)" -InputObject $content  
        $split = ($findReplacement.Matches.Value).Split(" ")  
        $i = 0  
        do {  
            $findBogdan = $split[$i]  
            $i++      
        } until ($findBogdan -eq "BOGDAN")  
        $replacementValue = ($split[1..($i - 2)]) -join ' '  
        $content.Replace("$replaceValue", "$replacementValue") | Out-File -FilePath $resultsdir\$($_.name)    
    }  
    

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten


9 additional answers

Sort by: Most helpful
  1. Suzana Eree 811 Reputation points
    2021-05-15T20:40:35.173+00:00

    sir, @Andreas Baumgarten Great answer. I modify your last code, both of the updates. One of the problem was the regex. Instead of "^*(BEBE).+(NERO)*" I put (BEBE).*(NERO) Same for the second regex.

    $sourcedir = "C:\Folder1\"  
     $resultsdir = "C:\Folder2\"  
     Get-ChildItem -Path $sourcedir -Filter *.html | ForEach-Object {  
         $content = Get-Content -Path $_.FullName -Raw  
         $findReplace = Select-String -Pattern "(BEBE).*(NERO)" -InputObject $content  
         $replaceValue = ($findReplace.Matches.Value).Split(" ")[1]      
         $findReplacement = Select-String -Pattern "(OANA).*(BOGDAN)" -InputObject $content  
         $replacementValue = ($findReplacement.Matches.Value).Split(" ")[1]  
         $content.Replace("$replaceValue", "$replacementValue") | Out-File -FilePath $resultsdir\$($_.name)    
     }  
    

    OR

    $sourcedir = "C:\Folder1\"  
     $resultsdir = "C:\Folder2\"  
     Get-ChildItem -Path $sourcedir -Filter *.html | ForEach-Object {  
         $content = Get-Content -Path $_.FullName -Raw    
         $replaceValue = ((Select-String -Pattern "(BEBE).*(NERO)" -InputObject $content).Matches.Value).Split(" ")[1]    
         $replacementValue = ((Select-String -Pattern "(OANA).*(BOGDAN)" -InputObject $content).Matches.Value).Split(" ")[1]  
         $content.Replace("$replaceValue", "$replacementValue") | Out-File -FilePath $resultsdir\$($_.name)    
     }  
    

    THE ONLY PROBLEM IN THIS CASE OF POWESHELL CODE IS:

    If I have

    BEBE ONE-1 NERO

    and 2 words between OANA and BOGDAN on the second line, such as:

    OANA XX TWO-2 BOGDAN

    in this case the result is BEBE XX NERO, but should be BEBE XX TWO-2 NERO

    So, the regex is parsing only one word between OANA and BOGDAN, but he should have taken everything between them


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.