Solved

replacing text within a text file

Posted on 2002-07-02
8
177 Views
Last Modified: 2013-11-18
hi everyone,

is there a way to open a text file, search for a text, and replace all that with an empty string?

for example i have the text in a file:

hello
hello world what's up
bye

i want to search for "hello world" and replace it with "blank/empty string" or delere that text from the file so at the end i'll have

hello
what's up
bye

thanks
0
Comment
Question by:urif
  • 4
  • 4
8 Comments
 
LVL 7

Expert Comment

by:Cynna
Comment Utility
This is very easy - there are lots of ways.
One of the easiest is using TStringList to grab file in
memory, and then using StringReplace procedure to replace
your text.
If you wish, I'll post you example that does this.
0
 

Author Comment

by:urif
Comment Utility
thx, please if you can post an example.
0
 

Author Comment

by:urif
Comment Utility
oh, i forgot to mention, i'll be using this in a console application, so a lot of the vcl components maybe are not available.
0
 
LVL 7

Accepted Solution

by:
Cynna earned 50 total points
Comment Utility
You'll need TStringList, which uses Classes, and StringReplace (defined in Sysutils). I presume this won't be a problem for console app.

Copy/Paste:
----------------

function FileTextReplace(FileName, OriginalString, ReplacementString: String): Boolean;
var i: Integer;
    s: String;
begin
 Result:=FALSE;
 with TStringList.Create do begin
     try
         if not FileExists(FileName) then Exit;
         // Grab file into TStringList:
         LoadFromFile(FileName);
         // Copy file contents to single string variable:
         s:=Text;
         // Replace string (look at help for StringReplace() function, for further info):
         s:=StringReplace(s, OriginalString, ReplacementString, [rfIgnoreCase]);
         // Copy modified file back to TStringList:
         Text:=s;
         // And finally save it to same file:
         SaveToFile(FileName);
         Result:=TRUE;
     finally
        Free;
     end;
 end;
end;


DEMO:
-------
(place Button1 on Form1)

procedure TForm1.Button1Click(Sender: TObject);
begin
  // Delete 'hello world' from C:\bla.txt:
  if FileTextReplace('C:\bla.txt', 'hello world', '') then ShowMessage('Replaced OK.')
                                                      else  ShowMessage('Error!');
end;
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:urif
Comment Utility
i refrace the question and let me know if i should put it as a different one, so you can get the points:


the function works fine, i modified it a bit to suit my program. the problem is that one of the strings i need to replace is random, meaning, it could be anything, it's not a fixed string. what i know is that it is enclosed like in HTML and XML inside <> and that the open tag is <name> and the close is </name>

so basically i need find the opening tag and delete the string beginning (and including) the open tag all the way up to the close tag. example:

line 1 some text
line 2<mytag>some random string</mytag>
line 3 some other text

i need to delete the whole thing at the end i'll have

line 1 some text
line 2
line 3 some other text
0
 
LVL 7

Expert Comment

by:Cynna
Comment Utility
urif,

Oh well...this is a bit more complicated then what you
initially stated.
I can write you a function that will accept input MyTag,
and delete everything in between (including tags), like
you specified.

To be clear, take few examples:

a) file1.txt looks like this:

line 1 some text
line 2<title>some random string</title>
line 3 some other text

b) file2.txt looks like this:

line 1 some text
line 2<title>some random string</title>
line 3<bla>text</bla> text2 <blabla> text3 <blabla>


Lets take our function declaration for example:
 CutTags(FileName, TagName: String)

Then, a call like this:

a)  CutTags('file1.txt', 'title') would change file1.txt to this:
line 1 some text
line 2
line 3 some other text

b)  CutTags('file2.txt', 'bla') would change file1.txt to this:

line 1 some text
line 2<title>some random string</title>
line 3 text2 <blabla> text3 <blabla>


If you can wait a day or two, I'll write this for 50 points.
If you want it ASAP, you'll get it in about 1 hour for 100 points.

0
 

Author Comment

by:urif
Comment Utility
it's ok thx so much!

i'll figure it out.

actually i placed another question before placing this one here and i think it was answered.

again, thanks.
0
 
LVL 7

Expert Comment

by:Cynna
Comment Utility
No problem, glad I could help....
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now