batch file - use find/replace with formula -



batch file - use find/replace with formula -

this simple question, here goes.

i need search file numbers in sequence, add together 5 each number/ ex:

time hr:1,st:2,ps:10 (is looking - there kinds of other numbers throughout file)

i replace numbers +5

time hr:6,st:7,ps:15

batch not optimal selection text processing, complex requirement. done much more using jscript, vbscript, or powershell. possible batch...

there not batch command can want. have write own code read each line, , if not match format, write out unchanged. if match format, parse line extract numbers, utilize set /a increment them, , write out new line.

you cannot modify file directly. instead must write temp file. when finished, move temp file original name.

i assume formatted text of involvement not embedded within larger line.

you give no indication other content within file. blank lines , perhaps exclamation points can cause issues. i'll assume may present, code more complicated needed if not.

i protect empty lines using findstr /n insert line number prefix each line. prefix must stripped before writing out line new file.

exclamation points protected toggling delayed expansion on , off within loop. if delayed expansion enabled when variable expanded, value corrupted if contained exclamation point.

@echo off setlocal disabledelayedexpansion >"yourfile.txt.new" ( /f "delims=" %%l in ('findstr /n "^" "yourfile.txt"') ( /f "delims=:, tokens=1-7*" %%a in ("%%l") ( if "%%b %%d %%f%%h" equ "time hr st ps" ( set /a "hr=%%c+5, st=%%e+5, ps=%%g+5" setlocal enabledelayedexpansion echo time hr:!hr!,st:!st!,ps:!ps! endlocal ) else ( set "ln=%%l" setlocal enabledelayedexpansion echo(!ln:*:=! endlocal ) ) ) ) ::move /y "yourfile.txt.new" "yourfile.txt" >nul type yourfile.txt.new

batch-file replace find formula

Comments

Popular posts from this blog

c# - ASP.NET MVC Sequence contains no matching element -

java - Parsing XML, skip certain tags -

rest - How to invalidate user session on inactivity in a stateless server? -