Move and replace text in quotes

This topic contains 3 replies, has 3 voices, and was last updated by  Everton 4 months ago.

  • Author
    Posts
  • #90004

    Everton
    Participant

    Hello folks,

    I'm trying to move any word after #DINDEX (unknown word) Line #8 up to Line #3, then replace the word in quotes after #BQUEUE with the new word in quotes.

    $Files = get-childitem c:\BDITest2\ -recurse -include *.ierl
    foreach ($File in $Files)
    {
    $line8 = (Get-Content $File -TotalCount 8)[-1] -replace "#DINDEX","#BQUEUE"
    $fileContent = Get-Content $File
    $fileContent[2] = "$line8"
    $fileContent[7] = "#DINDEX"
    $fileContent | Set-Content $file
    }

    Example:
    #BATCH
    #BLABEL "BDI_Cmore_Results_AH Oct 07 20"
    #BQUEUE "RELMAGQ" ———- Delete the second word
    #DOCUMENT
    #DINDEX GE
    #DINDEX MR1111111
    #DINDEX 119998891
    #DINDEX PFT ———- Move the second (Unknown word) to line 3 then place in quotes

  • #90008

    postanote
    Participant

    I am really not sure I follow your logic here, but, are you saying you are trying to from this...
    (Through not as elegant as it can be and only targeting one word)

    Clear-Host
    "Original file *********`n"
    ($BatchFileData = (Get-Content -Path 'D:\Scripts\PowerShellTips\BatchTextFIle.txt') )
    "`n"
    Write-Warning -Message 'Replacing line3 2nd word with 2nd word from line8 and removing 2nd word from line8'
    (
    $BatchFileData `
    -replace (($BatchFileData[2] -split ' ')[2],($BatchFileData[7] -split ' ')[2])) `
    -replace ($BatchFileData[7]),($BatchFileData[7] -split ' ')[0..1]

    Results

    Original file *********

    #BATCH
    #BLABEL "BDI_Cmore_Results_AH Oct 07 20"
    #BQUEUE "RELMAGQ" "BQUEUE2ndWord"
    #DOCUMENT
    #DINDEX GE
    #DINDEX MR1111111
    #DINDEX 119998891
    #DINDEX PFT "DDINDEX2ndWord"

    WARNING: Replacing line3 2nd word with 2nd word from line8 and removing 2nd word from line8
    #BATCH
    #BLABEL "BDI_Cmore_Results_AH Oct 07 20"
    #BQUEUE "RELMAGQ" "DDINDEX2ndWord"
    #DOCUMENT
    #DINDEX GE
    #DINDEX MR1111111
    #DINDEX 119998891
    #DINDEX PFT

  • #90010

    Sam Boutros
    Participant
    $Files = Get-ChildItem c:\BDITest2\ -Recurse -Include *.ierl
    foreach ($File in $Files) {
        $myContent = Get-Content $File
        if ($myContent.Count -ne 8) { throw "Error: file ($File) has ($($myContent.Count)) lines - expecting (8)" }
        $DesiredWord = $myContent[7].Split(' ')[1]
        "Identified desired word in line 8 as ($DesiredWord)" # You can comment this line
        $ReplacementLine3 = "#BQUEUE ""$DesiredWord"""
        "Changing line 3 from ($($myContent[2])) to ($ReplacementLine3)" # You can comment this line
        $myContent[2] = $ReplacementLine3
        "Writing back to file ($File)" # You can comment this line
        $myContent | Out-File $File -Force
    }
    
  • #90026

    Everton
    Participant

    Thank you @Sam, That worked perfectly!!!

You must be logged in to reply to this topic.