Petr Posted December 3, 2010 Share Posted December 3, 2010 Hello, I have text file with 9999 lines, for example text.txt, containing: ---start of file--- this is 0001. line with "some text string containing some number 25 and some number 63" this is 0002. line with "some text string containing some number 14 and some number 55" this is 0003. line with "some text string containing some number 7 and some number 13" . . this is 9999. line with "some text string containing some number 39 and some number 15" ---end of file--- I would like to create new file with using FileRead, StringRegExpReplace and FileWrite: ---start of new file--- this is 0001. line with "some text string containing sum of both numbers 88" this is 0002. line with "some text string containing sum of both numbers 69" this is 0003. line with "some text string containing sum of both numbers 20" . . this is 9999. line with "some text string containing sum of both numbers 54" ---end of new file--- pls. help me, thx. Link to comment Share on other sites More sharing options...
rudi Posted December 3, 2010 Share Posted December 3, 2010 Hi. Welcome to the forum Rule #1: Post *YOUR* code when asking a question. Here is my exception to this rule #include <file.au3> $File_in="C:\boot.ini" $File_out="C:\Line-Numbers-added.txt" $Content="" _FileReadToArray($File_in,$Content) for $i = 1 to $Content[0] $Pre="000" & $i $pre="This is line " & StringRight($pre,4) & ": " $Content[$i]=$pre & $Content[$i] Next _FileWriteFromArray($File_out,$Content,1) run ("notepad.exe " & $file_out) Regards, Rudi. Earth is flat, pigs can fly, and Nuclear Power is SAFE! Link to comment Share on other sites More sharing options...
GEOSoft Posted December 3, 2010 Share Posted December 3, 2010 Local $sOut = "", $iVal Local $sStr = "some text string containing some number 25 and some number 63" & @CRLF & "some text string containing some number 20 and some number 60" Local $aStr = StringRegExp($sStr, "(?m:^|\n)\D*(\d+)\D*(\d+)\D*(?:\v|$)+", 3) If NOT @Error Then For $i = 0 To UBound($aStr) -2 Step 2 $iVal = $aStr[$i] + $aStr[$i +1] $sOut &= "The sum of " & $aStr[$i] & " + " & $aStr[$i+1] & " is " & $iVal & @CRLF Next EndIf MsgBox(0, "Test", $sOut) George Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.*** The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number. Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else. "Old age and treachery will always overcome youth and skill!" Link to comment Share on other sites More sharing options...
GEOSoft Posted December 3, 2010 Share Posted December 3, 2010 Here is a different method in case there are lines with no numbers in them Local $sOut = "", $iVal Local $sStr = "Some text string containing some number 25 and some number 63" & @CRLF & "Some text string containing no numbers" & @CRLF $sStr &= "Some text string containing some number 20 and some number 60" Local $aStr = StringRegExp($sStr, "(?m:^|\n)(.+)(?:\v|$)+", 3) If NOT @Error Then For $i = 0 To UBound($aStr) -1 Local $iVal = Execute(StringRegExpReplace($aStr[$i], "(?m:^|\n)\D*(\d+)\D*(\d+)\D*(?:\v|$)+", '"$1" + "$2"')) If $iVal Then $sStr = StringReplace($sStr, $aStr[$i], "This line was replaced with the total " & $iVal) Next EndIf MsgBox(0, "Test", $sStr) George Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.*** The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number. Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else. "Old age and treachery will always overcome youth and skill!" Link to comment Share on other sites More sharing options...
Petr Posted December 9, 2010 Author Share Posted December 9, 2010 Thanks, this is what i need Link to comment Share on other sites More sharing options...
Malkey Posted December 9, 2010 Share Posted December 9, 2010 Try this. expandcollapse popupOpt("WinTitleMatchMode", 2) ;1=start, 2=subStr, 3=exact, 4=advanced, -1 to -4=Nocase ; ======== Create & Display File ========= Local $sFile If FileExists("TestNums.txt") = 0 Then For $i = 1 To 9999 $sFile &= 'this is ' & StringRight("000" & $i, 4) & '. line with "some text string containing some number ' & _ Random(1, 99, 1) & ' and some number ' & Random(1, 99, 1) & '"' & @CRLF Next FileWrite("TestNums.txt", StringTrimRight($sFile, 2)) EndIf ShellExecute("TestNums.txt") WinWaitActive("TestNums.txt") Local $hWn = WinGetHandle("") MsgBox(0, "", "Press OK to continue", 0, $hWn) ; ======== FileRead ========= Local $sFileOrig = FileRead("TestNums.txt") ; ======== StringRegExpReplace ====== Local $sFile2 = Execute(StringTrimRight(StringRegExpReplace($sFileOrig, "(?:[^\d]*)(\d+)(?:[^\d]*)(\d+)(?:[^\d]*)(\d+).*\v*", _ '''this is \1. line with "some text string containing sum of both numbers '' & (\2 + \3) & ''"'' & @CRLF & '), 11)) ; ======= FileWrite ========== FileWrite("TestSum.txt", $sFile2) ; ====== Display & Delete the Created Files ======== ShellExecute("TestSum.txt") WinWaitActive("TestSum.txt") Local $hWn2 = WinGetHandle("TestSum.txt") Local $iRet = MsgBox(4, "Delete Files", "Press YES to delete the two file created", 0, $hWn2) If $iRet = 6 Then FileDelete("TestNums.txt") FileDelete("TestSum.txt") EndIf 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