giannis121 Posted November 19, 2010 Share Posted November 19, 2010 Hello friends! This is my first post here (so please be patient - i am not a professional). What i want to do is the following: I want to open a txt format file, find and replace some numbers. The syntax is not allways the same inside the text file. The numbers are allways after a specific latter. For example: T00 A100.2 B11.0 T01 A119.3 B211.8 C21.4 D21.0 So i would like to add certain values. For example after "A" i want to add +100.6 and after "B" +150.0. Finaly the file will look like: T00 A200.8 B161.0 T01 A219.9 B361.8 C21.4 D21.0 I have found in the forums some useful code for FileRead and FileReplace, but still not enough for me. Can anyone help? Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted November 19, 2010 Moderators Share Posted November 19, 2010 giannis121, Welcome to the AutoIt forum. Here is a way to do what you want: expandcollapse popup#include <Array.au3> $sInput = "T00 A100.2 B11.0" & @CRLF & "T01 A119.3 B211.8 C21.4 D21.0" ; Split into lines $aInput_Lines = StringSplit($sInput, @CRLF, 1) ; Take each line in turn For $i = 1 To $aInput_Lines[0] ; Split the line into elements $aInput_Items = StringSplit($aInput_Lines[$i], " ") ; This will hold the recreated line $sTempLine = "" ; For each element in the line For $j = 1 To $aInput_Items[0] Switch StringLeft($aInput_Items[$j], 1) ; if it begins with A Case "A" ; Remove the A $sTempItem = StringTrimLeft($aInput_Items[$j], 1) ; Convert the number into an actual number and add as required $iTempNumber = Number($sTempItem) + 100.6 ; Add to the new line with a trailing space $sTempLine &= "A" & $iTempNumber & " " Case "B" $sTempItem = StringTrimleft($aInput_Items[$j], 1) $iTempNumber = Number($sTempItem) + 150 $sTempLine &= "B" & $iTempNumber & " " Case Else $sTempLine &= $aInput_Items[$j] & " " EndSwitch Next ; Replace the exisitng line with the new one after removing the final space $aInput_Lines[$i] = StringTrimRight($sTempLine, 1) Next ; Convert the array of lines back into a string $sOutput = _ArrayToString($aInput_Lines, @CRLF, 1) ; Display it ConsoleWrite($sOutput & @CRLF) Please ask if you have any questions on what is going on in the script. You could also use an SRE - but that might be rushing things a bit! M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
giannis121 Posted November 19, 2010 Author Share Posted November 19, 2010 Thank you for the reply! Can you make it more specific for me, please? The input data is a file "test.txt" (see attached file) The values for adding to the existing ones are inside the "value.txt" The original text inside the "test.txt" will not be touched. Under it there will be the new calculated text. If you find it difficult to create it for me, please just give me the commands & functions that i may need to use. Thank you in advance. Giannistest.txtvalues.txt Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted November 19, 2010 Moderators Share Posted November 19, 2010 giannis121,- Use FileRead to get the text from the original file.- Use the code I gave you to amend the text.- Use FileWrite to append the new text to the file.You will now have a file which contains both sets of text.Try it yourself and come back if you have problems. M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
giannis121 Posted November 19, 2010 Author Share Posted November 19, 2010 Thank you very much! Works fine! Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted November 19, 2010 Moderators Share Posted November 19, 2010 giannis121, My pleasure! Always nice to help those who help themselves too! M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now