我正在使用Powershell在一些.txt文件中进行经典查找和替换。 如何最后保存文件?我尝试过|设置内容,但没有任何反应。 也许我需要先添加内容?
#Find what?
$optionBuilderStringToFind = "optionsBuilder.UseSqlServer"
$findUsingKeywordString = "using Microsoft."
#Replace with
$namespaceAdd = "using Microsoft.Extensions.Configuration;"
$optionBuilderConfigurable ="optionsBuilder.UseSqlServer(_configuration.GetConnectionString(`"Database`")
);"
gc -Path .\APSContext.cs | % {
if ($_ -match "using System;") {
$_ = $_ + "`n" + $namespaceAdd
#write-host $_
}
if ($_ -match "optionsBuilder.UseSqlServer") {
$_ = $optionBuilderConfigurable
#write-host $_
}
} | Set-Content -Path .\test.cs
更新:这是我要更改的testFile。文件的内容并不重要。我想添加另一个引用,例如“ using Something.Something”,并在文件中间将"optionsBuilder.UseSqlServer("dfjidfjljfiejf88");"
替换为"optionsBuilder.UseSqlServer("_configtest");"
:
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
namespace SRC.APS.Model.APSDB
{
public partial class APSContext : DbContext
{
public APSContext()
{
}
public APSContext(DbContextOptions<APSContext> options)
: base(options)
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlServer("dfjidfjljfiejf88");
}
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.HasAnnotation("ProductVersion", "2.2.0-rtm-35687");
}
也许我需要使用输出字符串代替?
答案 0 :(得分:1)
Powershell中的任何脚本块都可以产生值。这是通过 not 将值存储在变量中来完成的:
{ $foo = 3 }
什么也没产生{ 3 }
产生整数3
。以同样的方式
ForEach-Object { $_ = "something" }
什么也没产生ForEach-Object { $_ = "something"; $_ }
产生字符串"something"
您的循环主体不输出任何内容,就像上面的上例一样。因此,Set-Content
无关。修改块以实际返回更改后的$_
的值:
$replacements = @(
@{regex='using (System|Potentially|Others);'; replacement='using Microsoft.$1;' }
@{regex='optionsBuilder\.UseSqlServer\("[^"]*"\)'; replacement='optionsBuilder.UseSqlServer("Database")' }
# more search/replace pairs
)
Get-Content .\APSContext.cs -Encoding UTF8 | ForEach-Object {
foreach ($item in $replacements) {
$_ = $_ -replace $item.regex, $item.replacement
}
$_ # <--- this is what produces the line
} | Set-Content -Path .\test.cs -Encoding UTF8
话虽如此,在未指定文本编码的情况下,切勿加载或保存文本文件。对于C#源代码文件,我认为UTF-8是默认设置。
而且那,用正则表达式修改源代码不是一件好事。如果这是一次性的,那就好。如果您计划定期执行此操作,则说明您做错了什么。使用配置文件或环境变量,而不是保留经常更改的代码库文字值。
答案 1 :(得分:0)
switch
块可能会更好],消除不明智的$_
摆弄,然后确保总是一些输出。
# fake reading in a text file
# in real life, use Get-Content
$InStuff = @'
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
namespace SRC.APS.Model.APSDB
{
public partial class APSContext : DbContext
{
public APSContext()
{
}
public APSContext(DbContextOptions<APSContext> options)
: base(options)
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlServer("dfjidfjljfiejf88");
}
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.HasAnnotation("ProductVersion", "2.2.0-rtm-35687");
}
'@ -split [environment]::NewLine
#Find what?
$optionBuilderStringToFind = "optionsBuilder.UseSqlServer"
$findUsingKeywordString = "using Microsoft."
#Replace with
$namespaceAdd = 'using Microsoft.Extensions.Configuration;'
$optionBuilderConfigurable ='optionsBuilder.UseSqlServer(_configuration.GetConnectionString("Database"));'
$InStuff | ForEach-Object {
if ($_ -match "using System;")
{
$_ + "`n" + $namespaceAdd
#write-host $_
}
elseif ($_ -match "optionsBuilder.UseSqlServer")
{
$optionBuilderConfigurable
#write-host $_
}
else
{
$_
}
}
输出...
using System;
using Microsoft.Extensions.Configuration;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
namespace SRC.APS.Model.APSDB
{
public partial class APSContext : DbContext
{
public APSContext()
{
}
public APSContext(DbContextOptions<APSContext> options)
: base(options)
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlServer(_configuration.GetConnectionString("Database"));
}
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.HasAnnotation("ProductVersion", "2.2.0-rtm-35687");
}