Read from a file starting at the end, similar to tail

Error processing SSI file

Answers

  1. Khalil

    • 2021/1/15

    The code below uses a random-access FileStream to seed a StreamReader at an offset near the end of the file, discarding the first read line since it is most likely only partial.

    FileStream stream = new FileStream(@"c:\temp\build.txt", 
        FileMode.Open, FileAccess.Read);
    
    stream.Seek(-1024, SeekOrigin.End);     // rewind enough for > 1 line
    
    StreamReader reader = new StreamReader(stream);
    reader.ReadLine();      // discard partial line
    
    while (!reader.EndOfStream)
    {
        string nextLine = reader.ReadLine();
        Console.WriteLine(nextLine);
    }
    
  2. Cruz

    • 2015/4/9

    To read the last 1024 bytes: using (var reader = new StreamReader("foo.txt")) { if (reader.BaseStream.Length > 1024) { reader.BaseStream.

  3. Drew

    • 2017/9/18

    The code below uses a random-access FileStreamto seed a StreamReaderat an offset near the end of the file, discarding the first read line since it is most likely only partial. FileStream stream = new FileStream(@"c:\temp\build.txt", FileMode.Open, FileAccess.Read);stream.Seek(-1024, SeekOrigin.End); // rewind enough for > 1 lineStreamReader reader = new StreamReader(stream);reader.ReadLine(); // discard partial linewhile (!reader.EndOfStream){ string nextLine = reader.

  4. Kylen

    • 2017/7/20

    In native C#, how can I read from the end of a file? This is pertinent because I need to read a log file, and it doesn't make sense to read 10k, to read the 

  5. Justice

    • 2020/7/14

    Maybe something like this will work for you:

    using (var fs = File.OpenRead(filePath))
    {
        fs.Seek(0, SeekOrigin.End);
    
        int newLines = 0;
        while (newLines < 3)
        {
            fs.Seek(-1, SeekOrigin.Current);
            newLines += fs.ReadByte() == 13 ? 1 : 0; // look for \r
            fs.Seek(-1, SeekOrigin.Current);
        }
    
        byte[] data = new byte[fs.Length - fs.Position];
        fs.Read(data, 0, data.Length);
    }
    

    Take note that this assumes \r\n.

  6. Cole

    • 2019/11/2

    The Get-Content cmdlet always reads a file from start to finish. You can always get the very last line of the file like this: Get-Content -Path C:\Foo\BigFile.txt | Select-Object -Last 1 This is similar to the tail command in Linux. As is so often the case, the command doesn’t quite do what you want it to.

  7. Grant

    • 2021/3/19

    Read from a file starting at the end, similar to tail. Solution: To read the last 1024 bytes: using (var reader = new StreamReader("foo.txt")) { if (reader.

  8. Kyson

    • 2020/3/4

    while IFS= read -r line; do # done < <(tail -n "+$lineNumberIs" $dataFile) tail -n +K (with the plus sign) tells tail to start at the specified line number (see the man page). The <() bit is a process substitution. It lets you specify a command sequence and let bash read from it like a file.

  9. Walker

    • 2015/8/26

    To read the last 1024 bytes:

    using (var reader = new StreamReader("foo.txt"))
    {
        if (reader.BaseStream.Length > 1024)
        {
            reader.BaseStream.Seek(-1024, SeekOrigin.End);
        }
        string line;
        while ((line = reader.ReadLine()) != null)
        {
            Console.WriteLine(line);
        }
    }
    
  10. Nicholas

    • 2018/1/14

    10 Answers · 6. This not only goes to the end of the file, it waits for additional output at the end, like tail -f . · you're right @MichaelHampton. – Ruben 

  11. Smith

    • 2020/11/5

    Based on the answer I accepted, this works: tail -f -n +1 {filename} Why it works: The -f option continues to "follow" the file and output new lines as they are written to the file. The -n +1 instructs tail to start reading the file from the first line. Using -n -10 would start with the last ten lines of the file. tail.

  12. Bektashi

    • 2020/8/21

    It is a way to monitor the tail of a file which is growing while it is being viewed. (The behavior is similar to the "tail -f" command.).

  13. Martin

    • 2021/7/9

    Take a look at this related question's answer to read a text file in reverse. There is a lot of complexity to reading a file backward correctly because of stuff like encoding.

  14. Wes

    • 2021/10/27

    will go to the end and tail the file. From less man page: F Scroll forward, and keep trying to read when the end of file is reached. Normally this command would be used when already at the end of the file. It is a way to monitor the tail of a file which is growing while it is being viewed. (The behavior is similar to the "tail -f" command.)

  15. Issac

    • 2016/4/21

    tail is similar to the head utility, used for viewing the beginning of files. Syntax and Basic Usage. Tail uses the following basic syntax: tail 

  16. Pellegrini

    • 2019/6/29

    With this option tail command prints the data starting from specified line number of the file instead of end. For command: tail +n file_name, data will start printing from line number ‘n’ till the end of the file specified. $ tail +25 state.txt Telangana Tripura Uttar Pradesh Uttarakhand West Bengal 2.

  17. Javion

    • 2020/12/30

    The tail command writes the file specified by the File parameter to the file is read starting at the specified number of units from the end of the file.

  18. Corbin

    • 2017/4/23

    Note that a newline character counts as one byte. This command will display the last 93 bytes in the file: tail -c 93 list-2.txt. You can combine the -c (bytes) option with the + (count from the start of the file) modifier, and specify an offset in bytes counted from the start of the file: tail -c +351053 list-e.txt. Piping Into tail

  19. Parisi

    • 2017/1/19

    For command: tail +n file_name, data will start printing from line number 'n' till the end of the file specified. $ tail +25 state.txt Telangana 

  20. Ahmed

    • 2020/3/4

    ``-n +2'' starts the display at the second line of the input.</snip> -f The -f option causes tail to not stop when end of file is 

  21. Kash

    • 2015/7/30

    In Linux, getting a portion of text from input files is a common the tail command will print starting with the x-th line till the end of 

Comments are closed.

More Posts