IF there is one file for example test.config , this file contain work "WARN" between line 140 and 170 , there are other lines where "WARN" word is there , but I want to replace "WARN" between line 140 and 170 with word "DEBUG", and keep the remaining text of the file same and when saved the "WARN" is replaced by "DEBUG" between only lines 140 and 170 . remaining all text is unaffected.
6 Answers
Look at $_.ReadCount
which will help. Just as a example I replace only rows 10-15.
$content = Get-Content c:\test.txt
$content |
ForEach-Object {
if ($_.ReadCount -ge 10 -and $_.ReadCount -le 15) {
$_ -replace '\w+','replaced'
} else {
$_
}
} |
Set-Content c:\test.txt
After that, the file will contain:
1
2
3
4
5
6
7
8
9
replaced
replaced
replaced
replaced
replaced
replaced
16
17
18
19
20

- 28,745
- 11
- 71
- 104
-
I think I saw that first in some comment/post/tweet by @ShayLevy. – stej May 18 '11 at 19:26
-
Stej your answer worked perfectly and it is excatly what I need – Janki May 19 '11 at 00:03
-
@Janki, nice to hear that in the morning :) If it works, you may accept the answer. – stej May 19 '11 at 04:17
2 Lines:
$FileContent = Get-Content "C:\Some\Path\textfile.txt"
$FileContent | % { If ($_.ReadCount -ge 140 -and $_.ReadCount -le 170) {$_ -Replace "WARN","DEBUG"} Else {$_} } | Set-Content -Path "C:\Some\Path\textfile.txt"
Description:
- Write content of text file to array "$FileContent"
- Pipe $FileContent array to For-EachObject cmdlet "%"
- For each item in array, check Line number ($_.ReadCount)
- If Line number 140-170, Replace WARN with DEBUG; otherwise write line unmodified.
- NOTE: You MUST add the "Else {$_}". Otherwise the text file will only contain the modified lines.
- Set-Content to write the content to text file

- 123
- 3
Using array slicing:
$content = Get-Content c:\test.txt
$out = @()
$out += $content[0..139]
$out += $content[140..168] -replace "warn","DEBUG"
$out += $content[169..($content.count -1)]
$out | out-file out.txt

- 66,130
- 7
- 114
- 135
So my script is pretty similar, so I am going to post what I ended up doing.
I had a bunch of servers all with the same script in the same location, and I needed to updated a path in all of the scripts.
i just replaced the entire line (line 3 in this script) and rewrote the script back out
my server names and "paths" to replace the old path were stored in an array (you could pull that from a DB if you wanted to automated it more:
$servers = @("Server1","Server2")
$Paths = @("\\NASSHARE\SERVER1\Databackups","\\NASSHARE\SERVER2\Databackups")
$a = 0
foreach ($x in $servers)
{
$dest = "\\" + $x + "\e$\Powershell\Backup.ps1"
$newline = '$backupNASPath = "' + $Paths[$a] + '"'
$lines = @(Get-Content $dest)
$lines[3] = $newline
$lines > $dest
$a++
}
it works, and saved me a ton of time logging into each server and updating each path. ugh
Cheers

- 137
- 1
- 11
This is the test file
text
text
DEBUG
DEBUG
TEXT
--
PS:\ gc .\stuff1.txt |% { [system.text.regularexpressions.regex]::replace($_,"WARN","DEBUG") } > out.txt
Out.txt look like this
text text DEBUG
DEBUG
TEXT

- 25,014
- 6
- 48
- 78
Might be trivial but it does the job:
$content = gc "D:\posh\stack\test.txt" $start=139 $end=169 $content | % {$i=0;$lines=@();}{ if($i -ge $start -and $i -le $end){ $lines+=$_ -replace 'WARN', 'DEBUG' } else { $lines+=$_ } $i+=1 }{set-content test_output.txt $lines}

- 24,390
- 8
- 55
- 67