Search code examples
linuxcommand-linecygwin

How to crop(cut) text files based on starting and ending line-numbers in cygwin?


I have few log files around 100MBs each. Personally I find it cumbersome to deal with such big files. I know that log lines that are interesting to me are only between 200 to 400 lines or so.

What would be a good way to extract relavant log lines from these files ie I just want to pipe the range of line numbers to another file.

For example, the inputs are:

filename: MyHugeLogFile.log
Starting line number: 38438
Ending line number:   39276

Is there a command that I can run in cygwin to cat out only that range in that file? I know that if I can somehow display that range in stdout then I can also pipe to an output file.

Note: Adding Linux tag for more visibility, but I need a solution that might work in cygwin. (Usually linux commands do work in cygwin).


Solution

  • Sounds like a job for sed:

    sed -n '8,12p' yourfile
    

    ...will send lines 8 through 12 of yourfile to standard out.

    If you want to prepend the line number, you may wish to use cat -n first:

    cat -n yourfile | sed -n '8,12p'