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,407 questions
0 comments No comments
{count} votes

Accepted answer
  1. Andreas Baumgarten 98,626 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. Suzana Eree 811 Reputation points
    2021-05-15T14:56:18.487+00:00

    I tried, but the same result. Doesn't make any change in the html files. My powershell code below just copy the content from a folder to another keeping the same content. And I must modify those files, before copy to Folder2.

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

  2. Suzana Eree 811 Reputation points
    2021-05-15T16:23:15.757+00:00

    Your code is very good, sir @Andreas Baumgarten

    But there is a case to be considered. For example:

    text <p> BEBE ONE-1 NERO <div> text...  
          
    text <table> OANA TWO-2 BOGDAN <tr> text  
    

    AND

    text <p> BEBE GO-888 NERO <div> text...  
          
    text <table> OANA MYRY-78 BOGDAN <tr> text  
    

    AND

    text <p> BEBE LOVE-8 NERO <div> text...  
          
    text <table> OANA 456 BOGDAN <tr> text  
    

    As you can see, you help me with the case when the string TWO-2 has to replace "ONE-1". But every html file has another string that must be replaced.

    But all the files has the same structure. The string exception, what is different are the word between the same structure: XX and YY

    1. text <p> BEBE XX NERO <div> text...
    2. text <table> OANA YY BOGDAN <tr> text

    So, I must replace in all my html files, XX from line 1 with YY from line 6. And XX and YY are different words/strings in each file.

    0 comments No comments

  3. Suzana Eree 811 Reputation points
    2021-05-15T16:30:45.137+00:00

    Your code is very good, sir @Andreas Baumgarten

    But there is a case to be considered. For example:

    text <p> BEBE ONE-1 NERO <div> text...  
          
    text <table> OANA TWO-2 BOGDAN <tr> text  
    

    AND

    text <p> BEBE GO-888 NERO <div> text...  
          
    text <table> OANA MYRY-78 BOGDAN <tr> text  
    

    AND

    text <p> BEBE LOVE-8 NERO <div> text...  
          
    text <table> OANA 456 BOGDAN <tr> text  
    

    As you can see, you help me with the case when the string TWO-2 has to replace "ONE-1". But every html file has another string that must be replaced.

    But all the files has the same structure. The string exception, what is different are the word between the same structure: XX and YY

    1. text <p> BEBE XX NERO <div> text...
    2. text <table> OANA YY BOGDAN <tr> text

    So, I must replace in all my html files, XX from line 1 with YY from line 6. And XX and YY are different words/strings in each file.


  4. Andreas Baumgarten 98,626 Reputation points MVP
    2021-05-15T19:49:35.007+00:00

    @Suzana Eree ,

    please try this:

    $sourcedir = "./Junk/1"  
    $resultsdir = "./Junk/2"  
    Get-ChildItem -Path $sourcedir -Filter *a.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)    
    }  
    

    ----------

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

    Regards
    Andreas Baumgarten