find number set in text file, randomize and output new file

时间:2019-04-17 01:38:33

标签: batch-file

In a batch file is this possible ?

I need to be able to identify, change and randomize 2 numbers (X,Y) in a text file with all other numbers to remain as they are, then to output to new file.

Each set either starts with a number 1 then a space or a number 2 then a space. The numbers I want to change start after this, then the numbers after I do not want to change.

Example set:

2 315710.13 222342.17 570.00 0.0 0 1 1
2 316563.21 222546.17 570.00 0.0 0 1 1
2 315378.85 222092.42 570.00 0.0 0 1 1

Example set:

1 267976.30 237345.12 360.00 0.0 0 1 1
1 272779.45 239386.46 360.00 0.0 0 1 1
1 269837.52 239266.38 360.00 0.0 0 1 1

Example to change:

2 315710.13 222342.17 570.00 0.0 0 1 1

The example numbers I want to change are in bold. These are the 2 numbers I want to change throughout the file. I want these numbers to randomize within a range of 1000 + or -. Only these numbers I want to change and new file outputted with everything else unchanged.

Actual to change:

27_Static vehicles.artillery.Artillery$Flak18_88mm 2 315710.13 222342.17 570.00 0.0 0 1 1

copied from a simulator file.

1 个答案:

答案 0 :(得分:0)

@echo off
setlocal

set "input=a.txt"
set "output=b.txt"

rem If defined, these sections will be modified.
set modify_sections="[NStationary]" "[Buildings]"

rem If defined, these sections will be ignored.
@rem Any section defined in modify_sections will not be ignored.
set ignore_sections="[MAIN]"

rem Loop through lines from input file and save modified lines to output.
set "section="
(
    for /f "usebackq delims=" %%_ in ("%input%") do (
        set "line=%%_"
        call :section
        if errorlevel 1 (
            call :ignore
            if errorlevel 1 (

                @rem Get 1st 3 tokens from the line.
                for /f "tokens=1-3* delims= " %%A in ("%%_") do (
                    set "a=%%A" & set "b=%%B" & set "c=%%C"

                    @rem Get tokens from the previous remainder.
                    for /f "tokens=1-4* delims=. " %%a in ("%%D") do (
                        set "d=%%a" & set "e=%%b" & set "f=%%c" & set "g=%%d" & set "h=%%e"
                        call :modify
                    )
                )
            )
        )
    )
) 3> "%output%"
exit /b 0


:ignore
setlocal enabledelayedexpansion

rem Preset ignore_section default.
if defined modify_sections (set "ignore_section=1") else set "ignore_section="

rem Check if ignore this section.
for %%A in (%ignore_sections%) do if /i "%section%" == "%%~A" set "ignore_section=1"

rem Check if modify this section.
for %%A in (%modify_sections%) do if /i "%section%" == "%%~A"  set "ignore_section="

if not defined ignore_section exit /b 1

rem Ignoring line.
>&3 echo !line!
exit /b 0


:section
setlocal enabledelayedexpansion

rem Check for section name.
if not "!line:~,1!" == "[" exit /b 1

rem Found section name %line%.
>&3 echo !line!
endlocal & set "section=%line%"
exit /b 0


:modify
setlocal enabledelayedexpansion

rem d,f add or minus random number from original number.
if !random:~-1! gtr 4 (set /a "d+=!random:~,3!") else set /a "d-=!random:~,3!"
if !random:~-1! gtr 4 (set /a "f+=!random:~,3!") else set /a "f-=!random:~,3!"

rem e,g replace with random number and pad with 0 if needed.
set "e=!random:~-2!" & if "!e:~1!" == "" set "e=0!e!"
set "g=!random:~-2!" & if "!g:~1!" == "" set "g=0!g!"

rem Modified output.
>&3 echo   !a! !b! !c! !d!.!e! !f!.!g! !h!
exit /b 0

由于批处理文件不能使用浮点数,因此for循环 将以点和空格定界,从 2个浮点数。

基于

  

27_Static Vehicles.artillery.Artillery $ Flak18_88mm 2 315710.13 222342.17 570.00 0.0 0 1 1

a.txt是逐行读取的,最初没有定界。 call :section将检查是否有来自 检测到当前行,并将其保存到%section% 如果为true,则该行将立即回显到文件。

%%_分隔为:

  • a = %%A = 27_Static
  • b = %%B = vehicles.artillery.Artillery$Flak18_88mm
  • c = %%C = 2
  • * = %%D = 315710.13 222342.17 570.00 0.0 0 1 1

%%A%%C是令牌1-3,而%%D是令牌* 是其余的。

%%D分隔为:

  • d = %%a = 315710
  • e = %%b = 13
  • f = %%c = 222342
  • g = %%d = 17
  • h = %%e = 570.00 0.0 0 1 1

%%a%%d是令牌1-4,而%%e是令牌* 是其余的。

for变量设置为静态变量 即set "a=%%A"

%%D以外,所有令牌均设置为%a%-%h%的每一个。 %d%%f%的3位随机数, 添加到每个或从中减去。 %e%%g%均被随机替换 2个随机数的数字。

要处理输入文件中的节, 变量%modify_sections%%ignore_sections% 存在,因此可以按部分控制修改。 如果两个都未定义,则所有节都将被修改。 %modify_sections%的优先级高于%ignore_sections% 因此%modify_sections%的各个部分将始终是 改性。 %ignore_sections%在以下情况下很有用 %modify_sections%未定义。

每条修改的行都回显到%output%。流3是 用于回显到文件,这意味着您可以运行 包含echo on的脚本或向:modify添加回显, :section:ignore标签将被打印到 控制台而不是输出文件。