简体   繁体   中英

Windows Command Line: Search and replace value

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, ie 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM