hncl
hncl

Reputation: 2295

read a specific line from a file

I am trying to get a list of lines that are before lines that contains a specific word. Here is my script:

private static void Main(string[] args)
{
    int counter = 0;
    string line;

    System.IO.StreamReader file = new System.IO.StreamReader("E:\\overview2.srt");
    List<string> lines = new List<string>();
    while ((line = file.ReadLine()) != null)
    {
        if (line.Contains("medication"))
        {


            int x = counter - 1;
            Console.WriteLine(x); // this will write the line number not its contents

        }

        counter++;
    }

    file.Close();
}

Upvotes: 2

Views: 3832

Answers (4)

Zev Spitz
Zev Spitz

Reputation: 15317

Using LINQ method syntax:

var lines = File.ReadLines("E:\\overview2.srt")
        .Where(line => line.Contains("medication"))
        .ToList();

and LINQ keyword syntax:

var lines = (
    from line in File.ReadLines("E:\\overview2.srt")
    where line.Contains("medication")
    select line
).ToList();

If you need an array, use .ToArray() instead of .ToList().

Also, if all you need is to iterate once over the lines, don't bother with ToArray or ToList:

var query = 
    from line in File.ReadLines("E:\\overview2.srt")
    where line.Contains("medication")
    select line;
foreach (var line in query) {
    Console.WriteLine(line);
}

Upvotes: 2

KF2
KF2

Reputation: 10153

try this:

 int linenum = 0;
                foreach (var line in File.ReadAllLines("Your Address"))
                {
                    if (line.Contains("medication"))
                    {
                        Console.WriteLine(string.Format("line Number:{} Text:{}"linenum,line)
//Add to your list or ...
                    }
                    linenum++;
                }

Upvotes: 0

Nick Pearce
Nick Pearce

Reputation: 758

This code will show all lines immediately before any line that contains your search text.

    private static void Main(string[] args)
    {
        string cacheline = "";
        string line;

        System.IO.StreamReader file = new System.IO.StreamReader("C:\\overview2.srt");
        List<string> lines = new List<string>();
        while ((line = file.ReadLine()) != null)
        {
            if (line.Contains("medication"))
            {
                lines.Add(cacheline);
            }
            cacheline = line;
        }
        file.Close();

        foreach (var l in lines)
        {
            Console.WriteLine(l);           
        }
    }

It's difficult to tell from your question where you're looking for ALL lines before the found line or just a single line. (You would have to deal with the special case where the search text is found on the first line).

Upvotes: 0

Ian Mercer
Ian Mercer

Reputation: 39277

You could create a Queue<string>. Add each line to it as you pass over it. If it has more than the required number of lines in it dequeue the first item. When you hit your required search expression the Queue<string> contains all the lines you will need to output.

Or if memory is no object, you could just use File.ReadAllLines (see http://msdn.microsoft.com/en-us/library/system.io.file.readalllines.aspx) and index away into an array.

Upvotes: 0

Related Questions