3

I have ten text files (tab delimited, 200K rows). My intention is to seek characters [, ], | and replace them with a, o, u, respectively. Any hints how to do this using Windows batch script or Powershell?

jjoras
  • 2,001
  • 5
  • 18
  • 16

1 Answers1

12

This should take care of it, using Powershell. This can be done with straight cmd.exe stuff and some built-in Windows executables, but it would be far uglier and more difficult to comprehend.

It will read in some file, and on each line:

  • replace [ with a
  • replace ] with o
  • replace | with u

The escapes are needed since [, ], and | are all special characters in powershell, and the backtick ` is used to word-wrap commands.

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

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

If you want to process a list of files, you could set up an array to do this, and run through the array.

$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
}
wkl
  • 77,184
  • 16
  • 165
  • 176
  • 1
    You really shouldn't use the () around the Get-Content unless you .absolutely have to edit the file in place -- it causes all the lines to be collected before replacing, which is going to be really expensive with large files – Jaykul Nov 09 '10 at 17:23