0

I have few replacement using regex. For now I was using TextCrawler, but now I would like to do those replacement with bat file.

Could You help me make a syntax of my replacement so this bat file will do replacements in .txt files:

.first_text. replace with: example1

.secomd_text. replace with: example2

Peter_
  • 72
  • 1
  • 5
  • See [here](http://stackoverflow.com/a/16735079/2861476) – MC ND Mar 07 '15 at 11:57
  • Thanks mate but this is too much information for me. anything simplest – Peter_ Mar 07 '15 at 12:31
  • 1
    possible duplicate of [How can you find and replace text in a file using the Windows command-line environment?](http://stackoverflow.com/questions/60034/how-can-you-find-and-replace-text-in-a-file-using-the-windows-command-line-envir) – Mofi Mar 07 '15 at 12:38
  • Just do it with 'sed' from the GnuWin32 project. – Endoro Mar 07 '15 at 15:02
  • You should familiarize yourself with PowerShell. It can do regular expression search and replace and is a far more powerful and expressive language than cmd.exe shell script (batch). – Bill_Stewart Mar 07 '15 at 16:18

1 Answers1

0

Bill Stewart is right in this instance. The simplest answer without requiring a script or 3rd party utility is to use a PowerShell one-liner. To do a replacement in a single text file:

powershell "(gc textfile.txt) -replace '.first_text', 'example1' | sc textfile.txt"

To search and replace in all text files in the current directory:

powershell "gci *.txt | %{ (gc $_) -replace '.first_text', 'example1' | sc $_ }"

Or you could use jrepl.bat as MC ND suggests. Or you could use GnuWin32 sed as Endoro suggests.

rojo
  • 24,000
  • 5
  • 55
  • 101