0

I'm trying to add a line in a .sln file before the $pattern. The only problem is that when I try to add the $firstOccurrence condition in the if statement it doesn't add anything at all. It still triggers the Write-Debug.

The first occurrence part is now commented out but I can't seem to find out why it doesn't write anything when I set the first occurrence.

Original source to my solution can be found here: How to declare a variable and its type is Boolean in PowerShell?

    $firstOccurrence = $true;
    $pattern = "Global"
    (Get-Content $fileName) | Foreach-Object {      
        #if ($firstOccurrence) {
            if ($_ -match $pattern) {
                Write-Debug "test"
                $firstOccurrence = $false
                
                #Add Lines after the selected pattern 
                "aaaaaaaaaaaaaaaaaaaaaaa"
            }
        #}

        # send the current line to output
        $_

    } | Set-Content $fileName
  • Worked fine for me. So there is more than one 'global' in the file and that's why you're worried about the first occurrence? – Doug Maurer Dec 23 '20 at 16:44
  • Yes, when I leave out the `$firstOccurrence` It works fine and adds the line to all found lines. But when I add the condition and `$firstOccurrence` it doesn't add anything. – 5 Chameleons Dec 24 '20 at 07:02
  • Please be more exact in what you want as output. The title say to [1] add a **string before** the first occurrence of some pattern. The question itself says to [2] add a **line before** the pattern, while the comment in your code seems to explain you want to add a line **after** the pattern. I gave you an answer where [1] and [2] are handled, but so far you only reply that _it does not work_. [Edit](https://stackoverflow.com/posts/65427128/edit) your question and show what the output should look like. – Theo Dec 25 '20 at 12:16

1 Answers1

0

You could also do this using (very fast) switch -Regex like:

$fileName        = 'D:\Test\blah.txt'
$firstOccurrence = $true
$pattern         = "Global"
$insert          = "aaaaaaaaaaaaaaaaaaaaaaa"

$newContent = switch -Regex -File $fileName {
    $pattern { 
        if ($firstOccurrence) { 
            $insert
            $firstOccurrence = $false 
        }
        $_
    }
    default { $_ }
}
$newContent | Set-Content $fileName -Force

Or did you perhaps mean this:

$fileName        = 'D:\Test\blah.txt'
$pattern         = "Global"
$insert          = "aaaaaaaaaaaaaaaaaaaaaaa"

((Get-Content -Path $fileName -Raw) -split $pattern, 2) -join "$insert $pattern" | Set-Content $fileName -Force

?

Theo
  • 57,719
  • 8
  • 24
  • 41
  • It did not work. I'm getting the same result. – 5 Chameleons Dec 24 '20 at 16:03
  • @5Chameleons Well, that surprises me, because with my testfiles it works like a charm. Only above the first occurrence of `Global` the line is inserted. All other lines are left as-is, even though they can contain the word `Global` too. What exactly is not working? – Theo Dec 24 '20 at 18:40
  • Set-Content does not overwrite `$fileName`. It does add lines for every `Global` instance, but when I put the `$firstOccurrence = $false ` statement. It just keeps the old file for some reason. – 5 Chameleons Dec 29 '20 at 09:06
  • @5Chameleons Then add switch `-Force` so it can overwrite read-only files and make sure you don't have the file open in some blocking application at the time. You can also have it output to a **new** file which is always safer so it doesn't overwrite the original. – Theo Dec 29 '20 at 10:20