Solved

replacing text within a text file

Posted on 2002-07-02
8
182 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 7

Expert Comment

by:Cynna
ID: 7124551
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
ID: 7124574
thx, please if you can post an example.
0
 

Author Comment

by:urif
ID: 7124576
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
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 
LVL 7

Accepted Solution

by:
Cynna earned 50 total points
ID: 7124611
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
 

Author Comment

by:urif
ID: 7124720
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
ID: 7124854
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
ID: 7124875
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
ID: 7124900
No problem, glad I could help....
0

Featured Post

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

729 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