powershell - 用 Powershell 替换字符串
问题描述
我有一个 .properties 文件,我想在其中将字符串 Compass 替换为 BBB。我的问题是:我想提取属于
name
, JDBC/
,的字符串ds_name = ''
,java.lang.String
然后我将更新一个新的。顺便说一句,数据源名称不固定其动态变量。只是我把它写成示例字符串。
我尝试了以下 PowerShell 代码:
$DName = read-host -prompt "Please Enter Database Name"
ForEach ($client in (Get-Content Clients.txt)) {
(Get-Content "\\$client\D$\Runtime\run.properties") -replace "$old database name which is extract","$DName" |
Out-File "\\$client\D$\Runtime\run.properties"
}
运行属性:
dsid = AdminTask.createDatasource(provider_id, '[-name Compass -jndiName jdbc/Compass
-dataStoreHelperClassName com.ibm.websphere.rsadapter.MicrosoftSQLServerDataStoreHelper
-componentManagedAuthenticationAlias TEMP-HRZEMM01Node01/PlatformDataSource -containerManagedPersistence true
-xaRecoveryAuthAlias TEMP-HRZEMM01Node01/PlatformDataSource -configureResourceProperties [[databaseName java.lang.String Compass] [portNumber java.lang.Integer 1433] [serverName java.lang.String SQLSVR1]]]')
AdminConfig.create('MappingModule', dsid , '[[authDataAlias TEMP-HRZEMM01Node01/PlatformDataSource] [mappingConfigAlias ""]]')
ds_name = 'Compass' #Name copied from your question, update if required
解决方案
如果我正确理解了这个问题,您想首先找到Compass
存储在 .properties 文件中的数据库名称(可以是任何名称,只是一个示例),如果找到,则将其替换为在控制台中输入的值。
在这种情况下,我认为应该这样做:
$newDbName = Read-Host -prompt "Please Enter Database Name"
$clientFile = "Clients.txt"
ForEach ($client in (Get-Content $clientFile)) {
$content = Get-Content "\\$client\D$\Runtime\run.properties" -Raw
# see if we can extract the database name from the file
if ($content -match '(?:-name\s+|jdbc/|databaseName java\.lang\.String\s+|ds_name = '')(?<dbname>[^\s''\]]+)') {
$oldDbName = $matches['dbname']
Write-Host "Replacing '$oldDbName' with '$newDbName' for client '$client'"
($content -replace $oldDbName, $newDbName) |
Out-File "\\$client\D$\Runtime\run.properties"
}
else {
Write-Warning "Could not parse the old database name from '\\$client\D$\Runtime\run.properties'.."
}
}
正则表达式解释
(?: Match the regular expression below
Match either the regular expression below (attempting the next alternative only if this one fails)
-name Match the characters “-name” literally
\s Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.)
+ Between one and unlimited times, as many times as possible, giving back as needed (greedy)
| Or match regular expression number 2 below (attempting the next alternative only if this one fails)
jdbc/ Match the characters “jdbc/” literally
| Or match regular expression number 3 below (attempting the next alternative only if this one fails)
databaseName\ java Match the characters “databaseName java” literally
\. Match the character “.” literally
lang Match the characters “lang” literally
\. Match the character “.” literally
String Match the characters “String” literally
\s Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.)
+ Between one and unlimited times, as many times as possible, giving back as needed (greedy)
| Or match regular expression number 4 below (the entire group fails if this one fails to match)
ds_name\ =\ ' Match the characters “ds_name = '” literally
)
(?<dbname> Match the regular expression below and capture its match into backreference with name “dbname”
[^\s'\]] Match a single character NOT present in the list below
A whitespace character (spaces, tabs, line breaks, etc.)
The character “'”
A ] character
+ Between one and unlimited times, as many times as possible, giving back as needed (greedy)
)
推荐阅读
- python - 合并具有相同日期和 id 的行并附加到右侧
- java - 检查表是否为空的优化方法 SQLite
- wix - 如何使用 WIX 在 ExitDialog 中显示“SystemFolder”属性?
- matlab - 将matlab矩阵转换为fortran 90
- python - 将列合并为单列
- c# - 拦截响应时出现异常,当 ExceptionHandlerMiddleware 运行时
- charts - SuperSet Gamma 用户看不到自己的图表
- javascript - Javascript正文事件侦听器问题
- firebase - Flutter 使用 FutureBuilder 检索列表中的数据
- python - RobotFramework 从大量数字中获取每个单独的数字