查找和替换值
Find & replace values
工作代码:
Find Word "STIG" , remove it and save to .txt file
, I'm able to do it using following code:
$content | Where-Object { -not $_.Contains('STIG') } | set-content $file
问题:
我找不到单词“(non-R2)”并将其替换为“non-R2”并将值保存到文本文件。
SAMPLE:
CIS_Microsoft_Windows_Server_2008_R2_Benchmark_v3.2.0-xccdf.xml
CIS_Microsoft_Windows_Server_2012_(non-R2)_Benchmark_v2.2.0-xccdf.xml
CIS_Microsoft_Windows_Server_2012_R2_Benchmark_v2.4.0-xccdf.xml
CIS_Microsoft_Windows_Server_2016_RTM_(Release_1607)_Benchmark_v1.2.0-xccdf
CIS_Microsoft_Windows_Server_2016_STIG_Benchmark_v1.0.0-xccdf.xml
**Desired Result**
Will not contain line having "STIG" & will have replaced value "non-R2" (without brackets) and Save to Txt file:
CIS_Microsoft_Windows_Server_2008_R2_Benchmark_v3.2.0-xccdf.xml
CIS_Microsoft_Windows_Server_2012_non-R2_Benchmark_v2.2.0-xccdf.xml
CIS_Microsoft_Windows_Server_2012_R2_Benchmark_v2.4.0-xccdf.xml
CIS_Microsoft_Windows_Server_2016_RTM_(Release_1607)_Benchmark_v1.2.0-xccdf
((Get-Content -Path "sample.txt" ) -replace '\(non-R2\)','non-R2' ) | Set-Content "sample2.txt"
这会读取sample.txt
的内容,然后将“(non-R2)”替换为“non-R2”,然后将结果写入sample2.txt
。
工作代码:
Find Word "STIG" , remove it and save to .txt file , I'm able to do it using following code:
$content | Where-Object { -not $_.Contains('STIG') } | set-content $file
问题:
我找不到单词“(non-R2)”并将其替换为“non-R2”并将值保存到文本文件。
SAMPLE:
CIS_Microsoft_Windows_Server_2008_R2_Benchmark_v3.2.0-xccdf.xml
CIS_Microsoft_Windows_Server_2012_(non-R2)_Benchmark_v2.2.0-xccdf.xml
CIS_Microsoft_Windows_Server_2012_R2_Benchmark_v2.4.0-xccdf.xml
CIS_Microsoft_Windows_Server_2016_RTM_(Release_1607)_Benchmark_v1.2.0-xccdf
CIS_Microsoft_Windows_Server_2016_STIG_Benchmark_v1.0.0-xccdf.xml
**Desired Result**
Will not contain line having "STIG" & will have replaced value "non-R2" (without brackets) and Save to Txt file:
CIS_Microsoft_Windows_Server_2008_R2_Benchmark_v3.2.0-xccdf.xml
CIS_Microsoft_Windows_Server_2012_non-R2_Benchmark_v2.2.0-xccdf.xml
CIS_Microsoft_Windows_Server_2012_R2_Benchmark_v2.4.0-xccdf.xml
CIS_Microsoft_Windows_Server_2016_RTM_(Release_1607)_Benchmark_v1.2.0-xccdf
((Get-Content -Path "sample.txt" ) -replace '\(non-R2\)','non-R2' ) | Set-Content "sample2.txt"
这会读取sample.txt
的内容,然后将“(non-R2)”替换为“non-R2”,然后将结果写入sample2.txt
。