0

I'm trying to find and replace the value of something in a text file and am having trouble figuring out how I would do that.

For example, I would like to go into a file that has android:versionName="1.53" and replace it with android:versionName="1.54".

I know how to do this if I just want to replace 1.53 with 1.54 for example, using a program called FART (find and replace text) but I want to be able to replace whatever is in between the speech marks with 1.54, i.e. without having to specify 1.53 as this could change each time.

Another similar example would be replacing #define DLS_VERSION (1540) with #define DLS_VERSION (1600).

I was just wondering if there's a standard way or utility to do this please?

Thanks, Chris.

Chris Moore
  • 157
  • 1
  • 1
  • 12
  • SED is a stream editor that edits text files from the command line using regular expressions. A native Windows batch file that has similar functionality is `REPL.BAT` by dbenham from Stack Overflow. They are both robust methods, as plain batch files can be limited by the makeup of the file to be changed. – foxidrive Jan 07 '14 at 13:10

1 Answers1

0

Any regular expression (regex) search and replace utility should work just fine. There are many free options for Windows, though none native to batch.

One good option is REPL.BAT - a hybrid JScript/batch utility that works on any Windows machine from XP forward, and does not require download or installation of any executable.

Assuming REPL.BAT is in current directory, or better yet, somewhere within your PATH:

type "yourFile.txt"|repl "(android:versionName=\q).*?(\q)" "$11.54$2" x >"yourFile.txt.new"
move /y "yourFile.txt.new" "yourFile.txt" >nul

type "yourFile.txt"|repl "(#define DLS_VERSION \().*?(\))" "$11600$2" >"yourFile.txt.new"
move /y "yourFile.txt.new" "yourFile.txt" >nul

Full documentation is embedded within the REPL.BAT script. A simple web search can find many sites that explain regular expressions.

Community
  • 1
  • 1
dbenham
  • 119,153
  • 25
  • 226
  • 353
  • Apologies, I accidentally edited my last comment 5 times so couldn't edit anymore. Wow, thanks dbenham, that works perfectly! I think my main problem was not being able to think of what exactly to search for, i.e. "regular expressions" Just a couple of questions about the syntax of the repl.bat calls if that's OK? Does the .? part just mean whatever is between the 2 quotes? Is the x before the stdout part in example 1 because we're using the quote character in the find part? Does the \( in example 2 just evaluate to ( ? (it doesn't seem to be listed in the documentation with \\, \b etc) – Chris Moore Jan 07 '14 at 16:45
  • @ChrisMoore - `?` makes the `.*` expression non greedy, meaning that it stops matching as soon as it successfully matches the following expression (the concept is hard to explain). The `X` option is required to enable the `\q` syntax for a quote. Text between parentheses is captured so that it can be referenced in the replace string. `\(` and `\)` are simply escaped `(` and `)` characters that do not capture. Read the MicroSoft regex documentation that is available at the link I put in the REPL documentation. Also, don't forget to accept the best answer that meets your needs. – dbenham Jan 07 '14 at 17:04