Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Insert text after a match

$
0
0

My goal is to add text after the match. Howerve; the code below doesn't do anything. Apreciate your help.

Example:

Text before:

192.1.2.41 - - [09/Nov/2013:00:13:00]
10.32.1.52  - - [09/Nov/2013:00:13:00]
192.1.2.41 - - [09/Nov/2013:00:13:00]
178.19.3.39 - - [09/Nov/2013:00:13:00]
10.2.91.52 - - [09/Nov/2013:00:13:00]
10.2.91.52 - - [09/Nov/2013:00:13:00]


Text After:

192.1.2.41 - - [09/Nov/2013:00:13:00]
10.2.91.52 localhost  - - [09/Nov/2013:00:13:00]
192.1.2.41 - - [09/Nov/2013:00:13:00]
178.19.3.39 - - [09/Nov/2013:00:13:00]
10.2.91.52 - - [09/Nov/2013:00:13:00]
10.2.91.52 - - [09/Nov/2013:00:13:00]

code:

$fileName = "C:\scripts\access_combined.log"
[regex]$pattern='10\.2\.91\.52'

(Get-Content $fileName) | Foreach-Object  {
            $_

        if ($_ -match $pattern)
        {
          
         #add text aftre match
           
            $_ += "localhost"
          
        }
    } | Set-Content $fileName

 


Viewing all articles
Browse latest Browse all 21975

Trending Articles