Jump to content

Recommended Posts

Posted

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

  • Moderators
Posted

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:

  Reveal hidden contents

 

Posted
  On 8/22/2023 at 10:39 AM, 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)

 

Expand  

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.

Posted

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

Posted

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

Expand  

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.

Posted (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 by HezzelQuartz
  • Moderators
Posted

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:

  Reveal hidden contents

 

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
  • Recently Browsing   0 members

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