简体   繁体   中英

Powershell — replace string from one file into another

I have two files; A and B. They both contain similar text but of course there are subtle differences in each.

I need to replace a single line of text from file B that came from File A, leaving all the rest of the text in file B as is. The thing is that I don't know the full line of text that will exist in file A, just the first few letters.

Said another way:

I can get the single line of text(string) from file A: $a = (get-content $original_file)[5]

how do I replace line 5 of file B with what's in variable $A

thanks!

PowerShell arrays are zero-based so line 5 would be index 4. The rest of the script would be something like:

$b = (get-content $another_file)
$b[4] = $a
$b | Out-File -Encoding Ascii $another_file

You can pick Ascii or Unicode (or UTF8) for the encoding.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM