Solved

Search for text and delete from file

Posted on 2009-05-08
6
679 Views
Last Modified: 2012-05-06
How do I search for a string of text in a file, using wild card, and then delete the entire line from the file....

if line contains "My name is *", delete line from file
0
Comment
Question by:MichelleLacy
6 Comments
 
LVL 20

Expert Comment

by:ddayx10
ID: 24341899
You might try looking at this post, it gives pretty sound idea of the limitations of what you are trying to do and a solution:

http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/VB_Controls/Q_21696282.html
0
 
LVL 13

Expert Comment

by:iHadi
ID: 24341906
Hi

try the following code (I'm assuming your not opening text files over 10s of megabytes):
string filePath = "your file path";
 
string fileContent = File.ReadAllText(filePath);
 
fileContent = Regex.Replace(fileContent, "My name is.*", "", RegexOptions.Multiline | RegexOptions.IgnoreCase);
 
File.WriteAllText("your file path", fileContent);

Open in new window

0
 
LVL 7

Expert Comment

by:zwei
ID: 24344365
iHadi, your code doesn't delete the whole line.

How about this modification:
string filePath = "file path";
string fileContent = File.ReadAllText(filePath);
String[] lines = fileContent.Split('\n');
fileContent = "";
 
foreach(String line in lines) 
{
    if (! Regex.Match(line, ".*My name is.*", RegexOptions.IgnoreCase).Success)
    {
        fileContent += line + '\n';
    }
}
 
File.WriteAllText(filePath, fileContent);

Open in new window

0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 9

Accepted Solution

by:
tculler earned 500 total points
ID: 24344986
Unfortunately, there is no way to actually "delete" something from a file. You have a few choices; I'll give you an un-optimized version of one of them (which will work for any size file, theoretically). Using Regex is somewhat overkill in this situation, "split" won't work correctly in case you have different types of line deliniations (\n, \r, etc), and File.ReadAllLines() will read any file into memory FIRST, potentially using up way to much memory. The solution below should be able to 'dodge' any of these potential problems. However, I did not optimize it completely because the goal of it is clarity. Modify it as you wish.
void RemoveFromFile(System.IO.FileInfo mySource)
{
    System.String output = mySource.FullName;
    mySource.MoveTo(mySource.Name + "_TEMP" + mySource.Extension);
 
    String line = null;
    using (System.IO.StreamReader reader = new System.IO.StreamReader(mySource.OpenRead()))
    using (System.IO.StreamWriter writer = new System.IO.StreamWriter(System.IO.File.OpenWrite(output)))
    {
        while (!reader.EndOfStream)
        {
            line = reader.ReadLine();
 
            if (!line.Contains("My name is "))
            { writer.WriteLine(line); }
        }
        writer.Flush();
    }
 
    mySource.Delete();
}

Open in new window

0
 
LVL 7

Expert Comment

by:zwei
ID: 24345038
@tculler: Good solution, reading and writing at the same time. I think I saw an example of that in ddayx10's link. I would still recommend to use regexes though (to allow for future modifications). This code would probably need to have .ToLower() on every line and "My name is ".
0
 
LVL 9

Expert Comment

by:tculler
ID: 24380749
I never check links, and question askers usually don't either. If they're asking a question, they should have already researched using such obvious resources.

I didn't do a ToLower(), or use regex, because I felt that from the question asked the problem was specific enough to target in on one thing, do that one thing, and do it very well. RegEx is awesome, but does require a bit of overhead when using it (though it will save time, and probably boost efficiency, in more complex situations). As long as you know this won't change, this solution is fairly optimized and expandable. Of course, if there is more content you must avoid, RegEx may become a necessity (though you would just replace the call to .Contains to a Regex call). Let me know if you'd like to see the implementation of my solution using RegEx, and I'll whip one up.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
New to SOAP and need some direction 10 79
WKHTMLTOPDF - --disable-smart-shrinking not working 10 84
Hey!!!! 1 20
tableview is not updating 1 7
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

785 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