Rskm Posted February 14, 2018 Share Posted February 14, 2018 I have a text file with lot of lines (of the order of 30000 - thats to get an idea about the size of file). i wish to replace particular texts , and wish to do the search based on the 'start' and 'end' of such lines , because the 'in between' portions are variables and they vary. the texts in the file looks like below CAD 100 101 102 E NEW REPLACE SOME TEXT HERE SOME TEXT HERE SOME TEXT HERE SOME TEXT HERE SOME TEXT HERE SOME TEXT HERE SOME TEXT HERE SOME TEXT HERE CAD 200 201 202 R CHK REPLACE SOME TEXT HERE SOME TEXT HERE SOME TEXT HERE CAD 300 301 302 E FEW REPLACE SOME TEXT HERE SOME TEXT HERE SOME TEXT HERE SOME TEXT HERE SOME TEXT HERE SOME TEXT HERE SOME TEXT HERE CAD 400 401 402 G COK REPLACE SOME TEXT HERE SOME TEXT HERE SOME TEXT HERE SOME TEXT HERE What i want to replace is only the lines which start with 'CAD' and end with 'REPLACE' in the text, the replacements are respectively CAD 600 111 112 E NEW REPLACE CAD 700 222 223 E NEW REPLACE CAD 800 333 334 E NEW REPLACE CAD 900 444 445 E NEW REPLACE my replace should look something like replace ("CAD 100 $var1 $var2 $var3 $var 4 "REPLACE") with ("CAD 100 $newvar1 $newvar2 $newvar3 $newvar 4 "REPLACE") where $var1,2,3,4 represents 100,102,E and NEW and $newvars are the replacements. thanks. Link to comment Share on other sites More sharing options...
Subz Posted February 14, 2018 Share Posted February 14, 2018 What are the delimiters? spaces or tabs? Link to comment Share on other sites More sharing options...
Rskm Posted February 14, 2018 Author Share Posted February 14, 2018 hi, they are spaces.. Link to comment Share on other sites More sharing options...
Subz Posted February 14, 2018 Share Posted February 14, 2018 Maybe something like: #include <Array.au3> #include <File.au3> #include <String.au3> Local $aArray, $sStart, $sEnd, $x = 0 Local $aVar[4] = [111, 112, "E", "NEW"] _FileReadToArray(@ScriptDir & "\Test.txt", $aArray) If @error Then Exit For $i = 1 To $aArray[0] $sStart = StringLeft(StringStripWS($aArray[$i], 3), 3) $sEnd = StringRight(StringStripWS($aArray[$i], 3), 7) If $sStart = "CAD" And $sEnd = "REPLACE" Then $aArray[$i] = $sStart & " " & $aVar[0] & " " & $aVar[1] & " " & $aVar[2] & " " & $aVar[3] & " " & $sEnd $aVar[0] += 111 $aVar[1] += 11 EndIf Next _ArrayDisplay($aArray) Link to comment Share on other sites More sharing options...
jchd Posted February 15, 2018 Share Posted February 15, 2018 You can use a regex for that. This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) Link to comment Share on other sites More sharing options...
Malkey Posted February 15, 2018 Share Posted February 15, 2018 Here is an example of a regex for that. expandcollapse popup#cs CAD 100 101 102 E NEW REPLACE SOME TEXT HERE SOME TEXT HERE SOME TEXT HERE SOME TEXT HERE SOME TEXT HERE SOME TEXT HERE SOME TEXT HERE SOME TEXT HERE CAD 200 201 202 R CHK REPLACE SOME TEXT HERE SOME TEXT HERE SOME TEXT HERE CAD 300 301 302 E FEW REPLACE SOME TEXT HERE SOME TEXT HERE SOME TEXT HERE SOME TEXT HERE SOME TEXT HERE SOME TEXT HERE SOME TEXT HERE CAD 400 401 402 G COK REPLACE SOME TEXT HERE SOME TEXT HERE SOME TEXT HERE SOME TEXT HERE #ce Local $newvar3 = "E", $newvar4 = "New" Local $TextFile = StringRegExpReplace(FileRead(@ScriptName), "(?is)^.*#cs\v+|\v+#ce.*", "") ; Get test data from the block comment. ConsoleWrite("Original Text:" & @CRLF & $TextFile & @CRLF & @CRLF) Local $s = "'" & StringRegExpReplace($TextFile, "(?im)^CAD(\s*)(\d+)(\s*)(\d+)(\s*)(\d+)(\s*)(\w+)(\s*)(\w+)(\s*)REPLACE\s*$", _ "' & 'CAD' & '${1}'& (${2} + 500) & '${3}' & (${2}/100 * 111) & '${5}' & (${2}/100 * 111 +1) & '${7}' & $newvar3 & '${9}' & $newvar4 & '${11}REPLACE' & '") & "'" ; Note: Backreferences ${1}, ${3}, ${5}, ${7}, ${9}, and ${11} are the spacings of the original text. ;ConsoleWrite($s & @CRLF) ; <---Execute() function parameter ConsoleWrite("------------------------------------" & @CRLF) ConsoleWrite("Replaced Text:" & @CRLF & Execute($s) & @CRLF) 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