Jump to content

[SOLVED]-Search for part of a line in text file and replace with new line.


Recommended Posts

Hi

I am trying to read a portion of a line in a text file and the replace the whle line.

I have tried _ReplaceStringInFile but that seems to on replace the the text serched for and not the whole line.

I have attached a sample of my code.

I am not expecting anyone to rewrite the code or fix it, just a nudge in the correct direction.

The text file given in the code is not the original so please excuse my $variables = "nonsense line"

$oldline = "ke is here", is "ke is here for a long time" in the txt file.

Thanks

Alistair

Search_for_line.au3

Edited by Wena
Link to comment
Share on other sites

  • Moderators

Wena,

Much simpler to read the file into an array, change the lines and then rewrite it:

#include <File.au3>

Global $aLines

$sOldLine = "Ke is here"
$sNewLine = "Wena is here now"
$sTestFile = ("testline.txt")

; Read the file into an array
_FileReadToArray($sTestFile, $aLines)

; Now loop through the array
For $i = 1 To $aLines[0]
    ; If the text is found
    If StringInStr($aLines[$i], $sOldLine) Then
        ; Change the whole line
        $aLines[$i] = $sNewLine
    EndIf
Next

; And now rewrite the file - note we need to exclude the [0] element as that is the line count
_FileWriteFromArray($sTestFile, $aLines, 1)

Please ask if you have any questions. ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Try Coordinating FileReadLine with _ReplaceStringInFile

Tip: Read the full line make the replacements wherever required and then write the whole line (not only the Replace Chars)

Regards

Phoenix XL

Edited by PhoenixXL

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Link to comment
Share on other sites

Hi Melba23

Thanks for the reply.

I have read through the help file on _FileReadFromArray and FileWriteToArray and now understand it.

What I do not understand is in the FOR loop there is no step just "$aLines[0] as a stop value.

@Phoenix XL: I cannot read the whole line as the last half of the line is unique.

I need to change a line in the txt file that has the same beginning but unique at the end with a line that will always be the same.

Is there no "wild cards" like in SQL "%ke is%"

Alistair

Link to comment
Share on other sites

  • Moderators

Wena,

in the FOR loop there is no step just "$aLines[0] as a stop value

And what does the Help file tell you about the array returned by _FileReadToArray? ;)

$aArray[0] will contain the number of records read into the array

Which is why we have to use the $iBase parameter in _FileWriteFromArray to make sure we do not write it back to the file. :)

All clear? :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Moderators

Wena,

My pleasure. ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Moderators

Wena,

Edit the first post and select the "Use Full Editor" option - then you can edit the title as well. Please just add "[sOLVED] to the title - do not replace it entirely. ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

And as the sun sets slowly in the west.

Opt("WinTitleMatchMode", -2) ;1=start, 2=subStr, 3=exact, 4=advanced, -1 to -4=Nocase

; -------- Create a Test File - "testline.txt" -------------
Local $sOrigContents = _
        "line 1" & @CRLF & _
        "line 2" & @CRLF & _
        "So, Ke is here then." & @CRLF & _
        "line 4"
Local $sTestFile =  "testline.txt"
Local $hFileOrig = FileOpen($sTestFile, 2)
FileWrite($hFileOrig, $sOrigContents)
FileClose($hFileOrig)
; -------- Test File Created --------------

Local $sOldLine = "Ke is here"
Local $sNewLine = "Wena is here now"
Local $sFileContents = FileRead($sTestFile)
;ConsoleWrite($sFileContents & @LF)

; -- Replace entire old line containing string, $sOldLine, with new line, $sNewLine, and re-write to file --
Local $hFile = FileOpen($sTestFile, 2)
FileWrite($hFile, StringRegExpReplace($sFileContents, "(?m)(^.*" & $sOldLine & ".*$)", $sNewLine))
FileClose($hFile)

; -------- Display the file and delete the file. ----------
ShellExecute($sTestFile)
;ConsoleWrite(StringRegExpReplace($sTestFile,"(^.*\\)","") & @LF)
WinWaitActive(StringRegExpReplace($sTestFile,"(^.*\\)",""))
FileDelete($sTestFile) ; <------- Test File Deleted. -------------
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...