Jump to content
tonycst

how to replace different values in a large text file ?

Recommended Posts

tonycst

Hey guys i have a file with 366 lines that has text similar to this below:

HornSize=100
HornWeight=8
KeratinSize=500
KeratinWeight=0.01
LeechBloodSize=250
LeechBloodWeight=0.2
MetalSize=1000
MetalWeight=1

I am trying to replace every Size value by abstracting or multiplying it by a number.
How do i go about replacing every string that contains Size=**** with that value substracted by 5 for example ?
Do i need to read each line of the text or what ?

Any code example would help.

thanks !

 

Share this post


Link to post
Share on other sites
Bilgus

well its pretty obvious that you will have to go over every line but as far as pulling out the data I think a repexp function would be your best bet

Something like regexpreplace($line,(?i)(.*size.*?=.*?)(\d++), ${1}${2}* $multiplier)

 

 

Share this post


Link to post
Share on other sites
mikell
10 hours ago, tonycst said:

How do i go about replacing every string that contains Size=**** with that value substracted by 5 for example ?

Here it is

$txt = "HornSize=100" & @crlf & _ 
    "HornWeight=8" & @crlf & _ 
    "KeratinSize=500" & @crlf & _ 
    "KeratinWeight=0.01" & @crlf & _ 
    "LeechBloodSize=250" & @crlf & _ 
    "LeechBloodWeight=0.2" & @crlf & _ 
    "MetalSize=1000" & @crlf & _ 
    "MetalWeight=1"

$add = -5
$res = Execute("'" & StringRegExpReplace($txt, "Size=\K(\d+)", "' & $1+$add & '") & "'") 
Msgbox(0,"", $res)

Works for any "...Size" integer value in the text, and if the value to be substracted is constant  :) 

Share this post


Link to post
Share on other sites
tonycst

I did it different.
 

#include <File.au3>
#include <GUIConstantsEx.au3>
$GUI = GUICreate ("program",200,200)
GUICtrlCreateGroup ("Select INI file",5,5,190,40)
$fileinput = GUICtrlCreateInput ("",10,20,140,20)
$browse = GUICtrlCreateButton ("Browse",150,20,40,20)
GUICtrlCreateGroup ("Chose operation to perform",5,50,190,90)
$substract = GUICtrlCreateRadio ("substract",10,65,60)
$multiply = GUICtrlCreateRadio ("multiply",110,65,60)
GUICtrlCreateLabel ("That many times" ,10,92,90)
$amount = GUICtrlCreateInput ("10",90,90,30,20)
$size = GUICtrlCreateCheckbox ("Effect Size",10,110,80,20)
$weight = GUICtrlCreateCheckbox ("Effect Weight",110,110,80,20)

$start = GUICtrlCreateButton ("start",0,160,200,40)
GUISetState(@SW_SHOW, $GUI)
While 1
$msg = GUIGetMsg()
if $msg = $GUI_EVENT_CLOSE then Exit
if $msg = $browse Then
$FileOpen = FileOpenDialog ("Select INI file","", "INI (*.ini)")
If $FileOpen > "" Then GUICtrlSetData ($fileinput,$FileOpen)
EndIf
if $msg = $start Then _goforit()
WEnd
Func _goforit()
$INI = GUICtrlRead ($fileinput)
$LastLine = _FileCountLines ($INI)
$num = GUICtrlRead ($amount)
For $i = 1 to $LastLine
$ReadLiine = FileReadLine ($INI,$i)
If GUICtrlRead ($size) = $GUI_CHECKED Then
if StringInStr ($ReadLiine,"Size=") > 0 Then
$Value = StringTrimLeft ($ReadLiine,StringInStr ($ReadLiine,"="))
If GUICtrlRead ($substract) = $GUI_CHECKED Then
$NewString = StringReplace ($ReadLiine,$Value,$Value/$num)
_FileWriteToLine($INI,$i,$NewString,True)
EndIf
If GUICtrlRead($multiply) = $GUI_CHECKED Then
$action = "*"&$num
$NewString = StringReplace ($ReadLiine,$Value,$Value*$num)
_FileWriteToLine($INI,$i,$NewString,True)
EndIf
EndIf
EndIf
If GUICtrlRead ($weight) = $GUI_CHECKED Then
if StringInStr ($ReadLiine,"Weight=") > 0 Then
$Value = StringTrimLeft ($ReadLiine,StringInStr ($ReadLiine,"="))
If GUICtrlRead ($substract) = $GUI_CHECKED Then
$NewString = StringReplace ($ReadLiine,$Value,$Value/$num)
_FileWriteToLine($INI,$i,$NewString,True)
EndIf
If GUICtrlRead($multiply) = $GUI_CHECKED Then
$action = "*"&$num
$NewString = StringReplace ($ReadLiine,$Value,$Value*$num)
_FileWriteToLine($INI,$i,$NewString,True)
EndIf
EndIf
EndIf
Next
MsgBox (0,'','done','',$GUI)
EndFunc

regex is just too confusing for me at this time.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×