Regex Replace from Command line

I've written a free command line tool for Windows to do this. It's called rxrepl, it supports Unicode and file search. Some may find it a useful tool.

rxrepl is a Microsoft Windows command line tool to search and replace text in text files using Perl compatible regular expressions (PCRE).

It has the following features:

  • Search using Perl Compatible Regular Expressions
  • Use group matching in replacement text
  • Supports Windows and Unix line endings
  • Unicode support
  • Accepts multiple search/replace arguments
  • Options may be provided in an options file
  • Scan for files
  • Preview mode
  • Line and full file matching modes

enter image description here


The Scripting Guy covers how to do this in PowerShell (no additional downloads on most recent Windows OSs, you probably already have it installed).

Start it up, run the following (to replace a * with a @):

(Get-Content C:\Scripts\Test.txt) | 
Foreach-Object {$_ -replace "\*", "@"} | 
Set-Content C:\Scripts\Test.txt

This supports .NET regular expressions, including positive and negative look-ahead, and all manner of things notepad++ did not support with regex before version 6.x (as much as I love notepad++).


go here
http://gnuwin32.sourceforge.net/packages.html
scroll down to SED. Download coreutils too while you're at it.

this command will replace a with b globally, and on each line. so not just the first occurrence on the line.

e.g. using sed "s/a/b/g" a.txt

C:\>type a.txt
aaaa
aaa

C:\>sed "s/a/b/" a.txt
baaa
baa

C:\>sed "s/a/b/g" a.txt
bbbb
bbb

C:\>

VBScript does support regular expressions, you can do find and replace with it.

dim re, s
set re = new RegExp

re.Pattern="in"
re.Global=True 'false is default
s="bin din in"
MsgBox re.Replace(s,"xxx")

Displays bxxx dxxx xxx