Link to home
Start Free TrialLog in
Avatar of ntr2def
ntr2def

asked on

powershell script to edit text

I've always been able to edit a script to replace something with something else for example

$Files=get-childitem . server.xml -rec
foreach ($file in $Files)
{
(Get-Content $file.PSPath) |
Foreach-Object {$_ -replace 'directory="logs"', 'directory="E:/logfiles/TomcatLogs/AccessLog"'} |
Set-Content $file.PSPath
}

Always works but today I came across replacing text that had special characters in such as:


$Files=get-childitem . logging.properties -rec
foreach ($file in $Files)
{
(Get-Content $file.PSPath) |
Foreach-Object {$_ -replace "'directory = ${catalina.base}/logs'", "directory = E:/logfiles/TomcatLogs"} |
Set-Content $file.PSPath
}

When i run that it does nothing, edits nothing. Can anyone help?
Avatar of Aard Vark
Aard Vark
Flag of Australia image

The problem is the $ and { } characters. Escape these characters in your regular expression so that they become literal characters.

$Files=get-childitem . logging.properties -rec
foreach ($file in $Files)
{
(Get-Content $file.PSPath) |
Foreach-Object {$_ -replace "'directory = \$\{catalina.base\}/logs'", "directory = E:/logfiles/TomcatLogs"} |
Set-Content $file.PSPath
}

Open in new window

Avatar of ntr2def
ntr2def

ASKER

I got that but that's the original txt in the file which I'm trying to edit out.
Avatar of ntr2def

ASKER

Didn't see the code snippet. Let me test
Avatar of ntr2def

ASKER

it didnt work, no change in the document after i ran it
I can't full replicate what you're doing. But I've added a text file with the contents:

Line 1
String Begin 'directory = ${catalina.base}/logs' String End
Line 3

And run the following.

$files = ,(Get-ChildItem $env:temp | ? {$_.name -eq "test.txt"})
foreach ($file in $files)
{
  $content = Get-Content $file.fullname
  $content = $content | ForEach-Object {$_ -replace "'directory = \$\{catalina.base\}/logs'", "directory = E:/logfiles/TomcatLogs"}
  Set-Content $file.fullname -Force -Value $content
}

Open in new window

This is working and putting out the expected output.
Avatar of ntr2def

ASKER

it does work for a one liner i tried this approach with multiple instances for exmaple

Line 1
common.loader=${catalina.base}/lib,${catalina.base}/lib/*.jar,${catalina.home}/lib,${catalina.home}/lib/*.jar

Line 2

line 3

$Files=get-childitem . catalina.properties -rec
foreach ($file in $Files)
{
(Get-Content $file.PSPath) | 
Foreach-Object {$_ -replace "common.loader=\$\{catalina.base\}/lib,\$\{catalina.base\}/lib/*.jar,\$\{catalina.home\}/lib,\$\{catalina.home\}/lib/*.jar", "common.loader=\$\{catalina.base\}/lib,\$\{catalina.base\}/lib/*.jar,\$\{catalina.home\}/lib,\$\{catalina.home\}/lib/*.jar,E:/Environment/jotmlib/*.jar"} | 
Set-Content $file.PSPath
}

Open in new window

OK so in that case a whole lot has to be done differently. Are catalina.base and catalina.home completely different values? If they are you will require multiple replace statements.
Avatar of ntr2def

ASKER

actually I'm trying to add:
,E:/Environment/jotmlib/*.jar

to the end of:

common.loader=${catalina.base}/lib,${catalina.base}/lib/*.jar,${catalina.home}/lib,${catalina.home}/lib/*.jar
ASKER CERTIFIED SOLUTION
Avatar of Aard Vark
Aard Vark
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ntr2def

ASKER

Worked perfectly and i appreciate your explanation thank you for this