multiplying lots of numbers

March 26, 2010 at 12:30:23
Specs: Windows XP
I need to change all numbers in a text file, multiplying them by 2, while keeping all the other text in place. The text is formatted like this:

<identifier>UIP_STATIC_TEXT</identifier>
<Parameters>
<x>786</x>
<y>395</y>
<width>190</width>
<height>26</height>
<UI text>UI_CUSTOM_BATTLE_STRENGTHS
verdana_med</font_id>
<align>centre</align>
</UI text>
<locked>false</locked>
</Parameters>
<object_data></object_data>
<object_id>empty_art</object_id>
</UI piece>
<UI piece>weaknesses_text
<identifier>UIP_STATIC_TEXT</identifier>
<Parameters>
<x>786</x>
<y>475</y>
<width>190</width>
<height>26</height>
<UI text>UI_CUSTOM_BATTLE_WEAKNESSES
verdana_med</font_id>
<align>centre</align>
</UI text>
<locked>false</locked>
</Parameters>

and

<region>AISA_off_over
<left>93</left>
<top>363</top>
<right>120</right>
<bottom>390</bottom>
</region>
<region>AISA_on
<left>0</left>
<top>378</top>
<right>27</right>
<bottom>405</bottom>
</region>

the whole file consists of over 10000 lines, and thus impossible to do manually, but I'm absolutely lost in finding a way to do this, so any help is really appreciated.


See More: multiplying lots of numbers

Report •


#1
March 26, 2010 at 17:18:13
i tested this on your data, seemed to work:
@echo off>testfil.o & setlocal enabledelayedexpansion
for /f "tokens=*" %%a in (testfil.htm) do (
for /f "tokens=2-3 delims=<>" %%b in ("%%a") do set test2=%%b
set /a test3=test2*2
set ff=%%a
if !test3! gtr 0 (call :aa) else (>> testfil.o echo.%%a)
)
goto :eof

:aa
set test4=!ff:%test2%=%test3%!
>>testfil.o echo.!test4!
::------ end -----
i would run a careful FC on it afterwards, there might be some mines in the harbor.
Only set up to work where number is:
<adff>123xxx
not: <adfa>ddd<adafda>123xx
or:
123<adff>...
ie: number must be second html-"token"


Report •

#2
March 26, 2010 at 18:39:54
when parsing file and doing manipulation on files, use a good file parsing tool, Perl/Python/gawk etc, even vbscript...here's one way using gawk. (see my sig for download link)
/[0-9]+/{
  o=$0
  gsub(/<.[^>]*>/,"",o)
  o=o*2
  $0=gensub(/(<.*>)([0-9]+)(<.*>)/, "\\1"o"\\3", 1, $0)
}

save the above as myscript.awk, then on command line

C:\test>gawk -f myscript.awk file
P_STATIC_TEXT</identifier>
<Parameters>
<x>1572</x>
<y>790</y>
<width>380</width>
<height>52</height>
<UI text>UI_CUSTOM_BATTLE_STRENGTHS
verdana_med</font_id>
<align>centre</align>
</UI text>
<locked>false</locked>
</Parameters>
<object_data></object_data>
<object_id>empty_art</object_id>
</UI piece>
<UI piece>weaknesses_text
<identifier>UIP_STATIC_TEXT</identifier>
<Parameters>
<x>1572</x>
<y>950</y>
<width>380</width>
<height>52</height>
<UI text>UI_CUSTOM_BATTLE_WEAKNESSES
verdana_med</font_id>
<align>centre</align>
</UI text>
<locked>false</locked>
</Parameters>

and

<region>AISA_off_over
<left>186</left>
<top>726</top>
<right>240</right>
<bottom>780</bottom>
</region>
<region>AISA_on
<left>0</left>
<top>756</top>
<right>54</right>
<bottom>810</bottom>

GNU win32 packages | Gawk


Report •

#3
March 26, 2010 at 18:48:57
@nbrane, although it might not happen for OP's data, your batch will fail for tags like
<x>395abc</x>
, where there is a number in front of alphabets. just something to ponder.

GNU win32 packages | Gawk


Report •

Related Solutions

#4
March 26, 2010 at 19:38:57
yeah, another assumption i made without knowing it. a lot depends on how strict or not strict the file format is. For serious parsing, i usually just write in a programming language because my regexp experience would fit into the period at the end of this sentence.

Report •


Ask Question