Jump to content

How to search what line a string is available in a text file and input a string just before that line?


Recommended Posts

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

  • Moderators

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

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

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 by ioa747

I know that I know nothing

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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)

 

I know that I know nothing

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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)

 

I know that I know nothing

Link to comment
Share on other sites

@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 by HezzelQuartz
Link to comment
Share on other sites

  • Moderators

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

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

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