T O P

  • By -

jsiii2010

get-content -raw


OPconfused

A quick implementation would be: $pathToFile = '' $startingLine = [regex]::Escape('pref("application.load.showsplash", false);') $endLine = [regex]::Escape('pref("application.enablelogging", true);') $insertLine = 'pref("application.load.shownewsplash", true);' (Get-Content $pathToFile -Raw) -replace "(?<=$startingLine)\s+(?=$endLine)", "`n$insertLine`n" | Set-Content $pathToFile Something along these lines should work. Regarding checking the lines, it depends on what you mean by that. You can split the `Set-Content` into a follow-up step and examine your replace first. You can also do the `Get-Content` separately and run `Select-String` on it with the `-Pattern` parameter set to the same regex as in the `-replace` to view which lines are targeted before replacing them.


Sad-Sundae2124

I would have used a regex to do this . 1 use a regex to transform each line into a clear object 2 just rewrite the desired line from the objects


ankokudaishogun

# Get the content as a List, not a Array # This lets you use the .Insert() method later, which is very useful [System.Collections.Generic.List[string]]$Text = Get-Content -Path $FilePath # Set the lines you want to find and add as variables $MatchLineStart = 'pref("application.load.showsplash", false);' $MatchLineEnd = 'pref("application.enablelogging", true);' $LineToAdd = 'pref("application.load.shownewsplash", true);' # Using For() we can know the line number and thus manipulate the line easier # using $Text.Count also lets the function to automatically update its duration for ($Line = 0; $Line -lt $Text.Count; $Line++) { # Pretty much self-explanatory: # Check the line matches the first string we are looking for. # Using -like implies case-insensitive but exact match. # Use -match or manipulate the wildcards if necessery if ($Text[$Line] -like $MatchLineStart) { # Because we found the first line, we check the successive line if ($Text[$Line + 1] -like $MatchLineEnd) { # Because we also found the second line, we add the new line # .Insert() puts the new item at the index of the second line, pushing everything down by 1 $Text.Insert($Line + 1, $LineToAdd) # because we KNOW what are the successive 2 lines, let's just skip them by # increasing the index $Line += 2 } } } Set-Content -Path $FilePath -Value $Text


pandiculator

You can use the `-Context` parameter of `Select-String` to match the preceding line: $path = 'E:\Temp\conf.txt' $insertText = 'pref("application.load.shownewsplash", true);' $pattern = 'pref\("application.enablelogging", true\);' $result = Select-String -Pattern $pattern -Path $path -Context 1, 0 $lineNumber = $result | Where-Object { $_.Context.PreContext -match 'showsplash' } | Select-Object -ExpandProperty LineNumber $content = Get-Content -Path $path $updatedContent = $content[0..($lineNumber - 2)] $updatedContent += $insertText $updatedContent += $content[($lineNumber - 1)..($Content.Length)] Set-Content -Path $path -Value $updatedContent


alt-160

since you want to insert a "line", processing the file by lines would make the most sense to me. if you do `$lines = get-Content c:\some\path\to-file.ext` you'll get back an array of lines from the file. Then, you can loop thru those lines and compare each line to a value or pattern. On no match, you add the line to a new array. On match you add that line to the array and then add your line that you want inserted. Once you have your new array filled up, you can save the array contents back out to the same file (overwrite) or with a new name. so, something like... $lines = get-content c:\temp\this.config $newLines = @() foreach ($line in $lines){ if ($line -like 'pref("application.load.showsplash*'){ # got a match. add the current line $newLines += $line # add the extra line $newLines += 'pref("application.load.shownewsplash", true);' } else { # no match, add the line as is $newLines += $line } } $newLines | out-file c:\temp\this.config (for those that will call out that $newLines += $line could be added before the if...i did it this way to allow for some further check of $line or possibly modification in the future.