开发者

Replacing a text at specified line number of a file using powershell

IF there is one file for exa开发者_运维百科mple 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.


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


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


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


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


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}


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜