Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
399 views
in Technique[技术] by (71.8m points)

powershell - Change Letter inside a test file

I have a file that has character that deems it production or test.

P=production T=test

select-string "\myfileserverfiles999.80214.va.000000373.20210122.173314.20210755" -pattern "T"

The above returns a True. I need to change that T to a P in the file

999.80214.va.000000373.20210122.173314.20210755:1:ISA*00* 00 ZZ*83-1002042 *ZZ*841469824 *210 123*0753*$*00501*000000209*0*T:

How do I search for the T in this string

question from:https://stackoverflow.com/questions/65933101/change-letter-inside-a-test-file

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

OK, with all the necessary infomration we gathered step by step I'd come up with this simple solution:

$Path = '\myfileserverfiles999.80214.va.000000373.20210122.173314.20210755'
$Content = Get-Content -Path $Path
$NewContent = 
(($Content | Select-Object -First 1) -replace '(?<=*)T(?=:$)','P'),
($Content | Select-Object -Skip 1)
Set-Content -Path $Path -Value $NewContent

It takes the content of the file, changes only the first line, combines this new first line with the original unchanged rest of the file and writes it back to the original file.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...