-2

I am looking to search a text file for a specific keyword let's say companyName.

The program will search the word and then print the line that the word is on, it will also then print the line below this one too. It should do this for every line that the word is on. I.E prints all lines that has companyName and the line below it. (Idealy this will be written to a .txt file)

--->Program prints

companyName: Tesco

Address: 31, stern street (the line immediately below)

--EDIT--

here is my now working code for anyone else that may find it useful:

static void Main(string[] args)
    {
        var sb = new StringBuilder();
        var lines = File.ReadAllLines(@"C:\file.txt");
        for (int i=0; i<lines.Length; i++)
        {
            var line = lines[i];
            if (line.IndexOf("companyName", StringComparison.CurrentCultureIgnoreCase) >= 0)
            {
                var nextLine = lines[i + 1];
                sb.AppendLine(line);
                sb.AppendLine(nextLine);
            }
        }

        File.WriteAllText(@"C:\out.txt", sb.ToString());

}

DaveSwans
  • 57
  • 1
  • 2
  • 10

2 Answers2

0

Maybe something like this?

using (var sr = new StreamReader(inFile))
   using (var sw = new StreamWriter(OutFile))
      while (!sr.EndOfStream)
      {
          var line1 = sr.ReadLine();
          if (line1.IndexOf(SearchString, StringComparison.OrdinalIgnoreCase) >= 0)
             continue;

          sw.WriteLine(line1);
          sw.WriteLine(sr.ReadLine());
      }

Disclaimer: Not tested, no error checking, and I'm not responsible for the people you maim and otherwise injure with this code

Zoe
  • 27,060
  • 21
  • 118
  • 148
TheGeneral
  • 79,002
  • 9
  • 103
  • 141
  • Yeah sure, why not. – TheGeneral Jun 20 '18 at 10:06
  • Because `File.ReadAllLines(…)` or `File.ReadLines(…)` if you don't want to read them all at once – stuartd Jun 20 '18 at 10:26
  • @stuartd What if its a 5 gig file :) – TheGeneral Jun 20 '18 at 10:27
  • That's an uncommon case but a valid point. The [docs for ReadLines](https://msdn.microsoft.com/en-us/library/dd383503(v=vs.110).aspx) say this - _"When you use ReadLines, you can start enumerating the collection of strings before the whole collection is returned; when you use ReadAllLines, you must wait for the whole array of strings be returned before you can access the array. Therefore, when you are working with very large files, ReadLines can be more efficient."_ - – stuartd Jun 20 '18 at 10:36
  • @stuartd thanks for that, truth be known i just had the code on my screen from another question. though it kind of does work neatly with a peace-meal approach for reading and writing. also if you wanted to get fancy for spead you could adjust buffersize, and advanced file options for performance and other shenanigans. for which are obviously not related to the OPS question – TheGeneral Jun 20 '18 at 10:51
0

You can try something along these lines where variable filePath contains path to your file and keyWord contains string that your are looking for:

        var keyWord = "companyName";
        var hasKeyword = false;
        foreach (var line in System.IO.File.ReadAllLines(filePath))
        {
            if (line.Contains(keyWord))
            {
                Console.WriteLine(line);
                hasKeyword = true;
                continue;
            }
            if (hasKeyword)
            {
                Console.WriteLine(line);
                hasKeyword = false;
            }
        } 
eren
  • 708
  • 8
  • 20