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());
}