DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

Author Topic: Batch find/replace multiple strings in a single text file  (Read 8,518 times)

wetsmellydog

  • Participant
  • Joined in 2007
  • *
  • Posts: 120
I work with CNC equipment that uses g-code to control the motion. It is a simple text file with machine specific codes and vector positioning. I use a program that outputs the code in a not so generic form and I also need to change some of the codes for use with a laser. I need to add spaces, replace characters ect and would like to do so with the least amount of manual repetition. I have used Notepad ++ but have not figured out how to accomplish this. Any ideas or programs to use would be great. Thanks!

4wd

  • Pedantic Old Bastard
  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,652
    • Coding Snacks
Re: Batch find/replace multiple strings in a single text file
« Reply #1 on: March 18, 2015, 01:30 AM »
If you have is some before/after examples we could probably generate the regex for global replacements within Notepad++.
"I will not follow where the path may lead, but I will go where there is no path, and I will leave a trail." - Muriel Strode, 1903

rjbull

  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 3,217
Re: Batch find/replace multiple strings in a single text file
« Reply #2 on: March 18, 2015, 09:44 PM »
You might try a free-standing bulk search-and-replace utility.  If you're happy with console mode and/or command lines, try the freeware MiniTrue, though on Windows it's probably better to use the version tweaked by Jason Hood here.  If you'd prefer WinGUI, take a look at the  two related programs HandyFile Find And Replace and Text Workbench.  I've used both MiniTrue and Text Workbench in the (distant) past.

AbteriX

  • Member #520
  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Posts: 1,150
Re: Batch find/replace multiple strings in a single text file
« Reply #3 on: March 19, 2015, 07:21 AM »
Such multiple replacements can be done also on console or via a script
by utilizing PowerShell, which is already there on each Win7/Win8 PC.

Examples:

$Origin = Get-Content .\test.txt

$LineWrap = $Origin -replace "(.{72}\b )", "`$1`r`n"

$Replacement = $LineWrap -replace "code\b", "program"

Set-Content .\test-new.txt -value $Replacement


 
Greetings, Stefan.

Original address: https://www.donationcoder.com/forum/index.php?topic=40446.0