Searching for strings in text files

I am trying to search for strings, line by line, in large text files.

Page 1 of 1

5 Replies - 1812 Views - Last Post: 21 November 2009 - 03:13 PM Rate Topic: -----

#1 erosenko   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 10-May 09

Searching for strings in text files

Post icon  Posted 21 November 2009 - 01:21 PM

Hi All -

Well I have another project that I am trying to work on. Here is the basic run down of what I need this to do.

Search line by line through some decent sized text files (10 - 20 megs a piece) and look at each line for a user inputed string. When I see that string I want to write the entire line out to a different text file. I have 3 txt boxes on a form. What are we searching for, where is the file to search in, and where to write the output. I have a good start at the code, but I am not getting anything to output yet. Clearly I am still pretty new to C#...


namespace txtFinder
{
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
		}

		private void exitToolStripMenuItem_Click(object sender, EventArgs e)
		{
			Application.Exit();

		}

		private void textBox2_TextChanged(object sender, EventArgs e)
		{

		}

		private void button1_Click(object sender, EventArgs e)
		{

			//declare the basic variables
			//text is the text we are looking for
			//path is where the file is
			//output is where we are going to write the lines to


			string text = txtString.Text;
			string path = txtSearch.Text;
			string output = txtOutput.Text;


			//call the method searchinfile and bring back the 3 variables
			searchinfile(text, path, output);


			MessageBox.Show("Done");


		}
   


		
		private void searchinfile(string text, string path, string output)
	{
		
		//this is the method for reading the file	
		StreamReader sr = new StreamReader(path);
		StreamWriter sw = new StreamWriter(output);


		//the current line
		string currentLine;
		//string searchString = text;
		bool foundText = false;

		do
		{
			currentLine = sr.ReadLine();
			if (currentLine != null)
			{
				foundText = currentLine.Contains(text);

			}

		}
		while (currentLine != null && !foundText);

		if (foundText)
		{
			sw.WriteLine(currentLine);
			

		}

			
	}
	
   }
}



I think I am getting close, but I would really appreciate a hand with this.

Thanks in advance!

Is This A Good Question/Topic? 0
  • +

Replies To: Searching for strings in text files

#2 Momerath   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1021
  • View blog
  • Posts: 2,463
  • Joined: 04-October 09

Re: Searching for strings in text files

Posted 21 November 2009 - 02:02 PM

Move the write into the loop (what if the string occurs multiple times in the file?) and remove the check in the while that says you found the string. Close your files when you are done.
Was This Post Helpful? 0
  • +
  • -

#3 erosenko   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 10-May 09

Re: Searching for strings in text files

Posted 21 November 2009 - 02:18 PM

Ok... Did that... But now I am just basically making a copy of the entire text file to the new text file (output). So, my code for the string to search for is off. Here is the revised code...


namespace txtFinder
{
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
		}

		private void exitToolStripMenuItem_Click(object sender, EventArgs e)
		{
			Application.Exit();

		}

		private void textBox2_TextChanged(object sender, EventArgs e)
		{

		}

		private void button1_Click(object sender, EventArgs e)
		{

			//declare the basic variables
			//text is the text we are looking for
			//path is where the file is
			//output is where we are going to write the lines to


			string text = txtString.Text;
			string path = txtSearch.Text;
			string output = txtOutput.Text;


			//call the method searchinfile and bring back the 3 variables
			searchinfile(text, path, output);


			MessageBox.Show("Done");


		}
   


		
		private void searchinfile(string text, string path, string output)
	{
		
		//this is the method for reading the file	
		StreamReader sr = new StreamReader(path);
		StreamWriter sw = new StreamWriter(output);


		//the current line
		string currentLine;
		//string searchString = text;
		bool foundText = false;

		do
		{
			currentLine = sr.ReadLine();
			if (currentLine != null)
			{
				foundText = currentLine.Contains(text);
				sw.WriteLine(currentLine);

			}

		}
		while (currentLine != null);
		//removed from the while line right above
		//&& !foundText



		//commented out
		//if (foundText)
		//{
			//sw.WriteLine(currentLine);
		//}
			
	}
	
   }
}



Was This Post Helpful? 0
  • +
  • -

#4 Momerath   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1021
  • View blog
  • Posts: 2,463
  • Joined: 04-October 09

Re: Searching for strings in text files

Posted 21 November 2009 - 02:26 PM

if (currentLine != null && currentLine.Contains(text)) {
	sw.WriteLine(currentLine);
}

You can get ride of the foundText variable.

This post has been edited by Momerath: 21 November 2009 - 02:27 PM

Was This Post Helpful? 0
  • +
  • -

#5 erosenko   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 10-May 09

Re: Searching for strings in text files

Posted 21 November 2009 - 02:54 PM

This is working great! Thanks for all the help.

One last question, I want to grab the line above the currentLine now. Specifically, I want the first 10 characters of the line above it (a date value).

Is there an easy way to do this?
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6259
  • View blog
  • Posts: 24,028
  • Joined: 23-August 08

Re: Searching for strings in text files

Posted 21 November 2009 - 03:13 PM

Save the last line into another variable before you read the current one?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1