HezzelQuartz Posted August 22, 2023 Share Posted August 22, 2023 How to search what line a string is available in a text file and input a string just before that line? If I have a text file containing ============================================================= arts & entertainment biographies & memoirs children's books computers & technology cooking, food & wine ============================================================= How can I search what line containing "children's books" and put "business & investing" before it so that it will be ============================================================= arts & entertainment biographies & memoirs business & investing children's books computers & technology cooking, food & wine ============================================================= example.txt Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted August 22, 2023 Moderators Share Posted August 22, 2023 HezzelQuartz, You could read the file into an array, add an element with the new data, and then sort the array before rewriting the file to disk. M23 HezzelQuartz 1 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...
Andreik Posted August 22, 2023 Share Posted August 22, 2023 $sToFind = "children's books" $sToAddbefore = "business & investing" $sData = FileRead('example.txt') $sNew = StringRegExpReplace($sData, '(?m)^(' & $sToFind & ')$', $sToAddbefore & @CRLF & @CRLF & '$1') ; You can write this to your file or whatever MsgBox(0, '', $sNew) HezzelQuartz 1 When the words fail... music speaks. Link to comment Share on other sites More sharing options...
ioa747 Posted August 22, 2023 Share Posted August 22, 2023 (edited) with StringReplace ( "string", "searchstring/start", "replacestring" [, occurrence = 0 [, casesense = 0]] ) $sData = FileRead(@ScriptDir & '\example.txt') $sNewData = StringReplace($sData, "children's books", "business & investing" & @CRLF & @CRLF & "children's books") ConsoleWrite($sNewData & @CRLF) Edited August 22, 2023 by ioa747 I know that I know nothing Link to comment Share on other sites More sharing options...
Andreik Posted August 22, 2023 Share Posted August 22, 2023 38 minutes ago, ioa747 said: with StringReplace ( "string", "searchstring/start", "replacestring" [, occurrence = 0 [, casesense = 0]] ) $sData = FileRead(@ScriptDir & '\example.txt') $sNewData = StringReplace($sData, "children's books", "business & investing" & @CRLF & @CRLF & "children's books") ConsoleWrite($sNewData & @CRLF) This works for his specific example but might lead to unexpected replaces since it doesn't check if the search string span an entire row. When the words fail... music speaks. Link to comment Share on other sites More sharing options...
ioa747 Posted August 22, 2023 Share Posted August 22, 2023 thanks for the tip, that's because I'm struggling with RegEx, I'm trying to make an alternative way, more friendly to a new user so that would be better? $sData = FileRead(@ScriptDir & '\example.txt') $sNewData = StringReplace($sData, "children's books" & @CRLF & @CRLF , "business & investing" & @CRLF & @CRLF & "children's books" & @CRLF & @CRLF) ConsoleWrite($sNewData & @CRLF) HezzelQuartz 1 I know that I know nothing Link to comment Share on other sites More sharing options...
Andreik Posted August 22, 2023 Share Posted August 22, 2023 It's better but not enough. Imagine you have another line containing "more children's book", the result will be Quote arts & entertainment biographies & memoirs business & investing children's books computers & technology more business & investing children's books cooking, food & wine Probably the best approach using just StringReplace() it's to check also for a CRLF (or some sort of new line indicator) before the search string. When the words fail... music speaks. Link to comment Share on other sites More sharing options...
ioa747 Posted August 22, 2023 Share Posted August 22, 2023 Probably the best approach using just StringReplace() agree I won't disagree with that $sData = FileRead(@ScriptDir & '\example.txt') $sNewData = StringReplace($sData, @CRLF & @CRLF & "children's books" & @CRLF & @CRLF , _ @CRLF & @CRLF & "business & investing" & @CRLF & @CRLF & "children's books" & @CRLF & @CRLF) ConsoleWrite($sNewData & @CRLF) HezzelQuartz 1 I know that I know nothing Link to comment Share on other sites More sharing options...
Andreik Posted August 22, 2023 Share Posted August 22, 2023 As long there is a consistency and after each line it's an empty line, this should be enough: $sData = FileRead(@ScriptDir & '\example.txt') $sNewData = StringReplace($sData, @CRLF & "children's books" & @CRLF, _ @CRLF & "business & investing" & @CRLF & @CRLF & "children's books" & @CRLF) ConsoleWrite($sNewData & @CRLF) HezzelQuartz 1 When the words fail... music speaks. Link to comment Share on other sites More sharing options...
HezzelQuartz Posted August 22, 2023 Author Share Posted August 22, 2023 (edited) @Melba23 and everyone Could you please explain how to "read the file into an array, add an element with the new data, and then sort the array before rewriting the file to disk. "? It's better if you could give the code or function name @Andreik Nice, Great, Thank you Could you please also explain so that I can learn it? (?m) What is the point of this function? Is it multiline? what's for? ^($sToFind)$ What is the function of ^ and () and $? $1 What is this function?? Thank you $sToFind = "children's books" $sToAddbefore = "business & investing" $sData = FileRead('example.txt') $sNew = StringRegExpReplace($sData, '(?m)^(' & $sToFind & ')$', $sToAddbefore & @CRLF & @CRLF & '$1') ; You can write this to your file or whatever MsgBox(0, '', $sNew) @ioa747 Thank you for this alternative solution Nice Edited August 22, 2023 by HezzelQuartz Link to comment Share on other sites More sharing options...
Andreik Posted August 22, 2023 Share Posted August 22, 2023 (?m) - is the modifier for multi-line mode ^ and $ - match the beginning and end of a line ( ) - denotes a capturing group $1 - it's a back-reference to the first captured group HezzelQuartz 1 When the words fail... music speaks. Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted August 22, 2023 Moderators Share Posted August 22, 2023 HezzelQuartz, If you look inside the Help file you will find the following functions: _FileReadToArray _ArrayAdd _ArraySort _FileWriteFromArray complete with examples of how to use them. I was rather hoping you might do that yourself once given the sequence of actions.... M23 HezzelQuartz and somdcomputerguy 1 1 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...
Nine Posted August 22, 2023 Share Posted August 22, 2023 You could also do it in a single line : _ReplaceStringInFile("Your file here.txt", @CRLF & "children's books" & @CRLF, @CRLF & "business & investing" & @CRLF & @CRLF & "children's books" & @CRLF, $STR_CASESENSE) ioa747 1 “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
HezzelQuartz Posted August 22, 2023 Author Share Posted August 22, 2023 @Andreik , @Melba23 Great, Nice, So much help, Thank you @Nine Thank you 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