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 Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,389 questions
0 comments No comments
{count} votes

Accepted answer
  1. Andreas Baumgarten 97,731 Reputation points MVP
    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. Andreas Baumgarten 97,731 Reputation points MVP
    2021-05-15T07:44:06.393+00:00

    Hi @Suzana Eree ,

    maybe this is helpful to get started:

    $a = "text text blah blah  
    BEBE ONE-1 NERO  
         
    text text  
         
    OANA TWO-2 BOGDAN  
    text text blah blah"  
      
    $a.Replace("ONE-1","TWO-2")  
    

    ----------

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

    Regards
    Andreas Baumgarten

    1 person found this answer helpful.

  2. Andreas Baumgarten 97,731 Reputation points MVP
    2021-05-15T15:10:37.613+00:00

    Hi @Suzana Eree ,

    Please try this, just modify the path to the files (replacement works here):

    $sourcedir = "./Junk/1"  
    $resultsdir = "./Junk/2"  
    Get-ChildItem -Path $sourcedir -Filter *.html | ForEach-Object {  
        $content = Get-Content -Path $_.FullName -Raw  
        $content.Replace("ONE-1", "TWO-2") | 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

    1 person found this answer helpful.

  3. Suzana Eree 811 Reputation points
    2021-05-15T14:46:39.62+00:00

    I made a powershell code. I must select and modify the files from Folder to and to copy the results to Folder2. I made the replacement just as @Andreas Baumgarten show me. But, my code is not doing the replacement. I have to change exactly those words in that location, without changing anything else around. Please help me with a better code.

    $sourcedir = "C:\Folder1\"  
    $resultsdir = "C:\Folder2\"  
     Get-ChildItem -Path $sourcedir -Filter *.html | ForEach-Object{  
     $output=@()  
     $content = Get-Content -Path $_.FullName  
    
    $a = "BEBE ONE-1 NERO         
       
    OANA TWO-2 BOGDAN"  
      
    $a.Replace("ONE-1","TWO-2")  
       
    $output += $content  
    
    $output | Out-File -FilePath $resultsdir\$($_.name)  
    }
    

  4. Andreas Baumgarten 97,731 Reputation points MVP
    2021-05-15T14:52:09.847+00:00

    Hi @Suzana Eree ,

    you can try to use the -Raw parameter for Get-Content:

      $content = Get-Content -Path $_.FullName -Raw  
    

    ----------

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

    Regards
    Andreas Baumgarten

    0 comments No comments