Jump to content
Rskm

search with start and end of a line

Recommended Posts

Rskm
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.

Share this post


Link to post
Share on other sites
Subz

What are the delimiters? spaces or tabs?

Share this post


Link to post
Share on other sites
Rskm

hi, they are spaces.. 

Share this post


Link to post
Share on other sites
Subz

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)

 

Share this post


Link to post
Share on other sites
jchd

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 here
RegExp tutorial: enough to get started
PCRE 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)

Share this post


Link to post
Share on other sites
Malkey

Here is an example of a regex for that.

#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)

 

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

×