Powershell or Batch: Find and Replace Characters

I have ten text files (with tab delimiters, 200 thousand lines). I intend to search for characters [,], | and replace them a, o, u, respectively. Any tips on how to do this using a Windows script package or Powershell?

+3
source share
1 answer

This should take care of this using Powershell. This can be done with simple material cmd.exeand some of the built-in Windows executables, but it would be much deeper and harder to understand.

It will be read in some file and in each line:

  • replace [witha
  • replace ]witho
  • replace |withu

escape-, [, ] | - powershell, ` .

$filename="textfile.txt"
$outputfile="$filename" + ".out"

Get-Content $filename | Foreach-object {
    $_ -replace '\[', 'a' `
       -replace '\]', 'o' `
       -replace '\|', 'u'
} | Set-Content $outputfile

, .

$filenames = @("/path/to/File1.txt", "file2.txt", "file3.txt")
foreach ($file in $filenames) {
    $outfile = "$file" + ".out"

    Get-Content $file | Foreach-object {
        $_ -replace '\[', 'a' `
           -replace '\]', 'o' `
           -replace '\|', 'u'
    } | Set-Content $outfile
}
+11

Source: https://habr.com/ru/post/1773671/


All Articles