Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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).

like image 752
bits Avatar asked Apr 15 '11 23:04

bits


People also ask

How cut a line from a file in Linux?

The cut command in UNIX is a command for cutting out the sections from each line of files and writing the result to standard output. It can be used to cut parts of a line by byte position, character and field. Basically the cut command slices a line and extracts the text.

How do you print a range of lines in Unix?

p - Print out the pattern space (to the standard output). This command is usually only used in conjunction with the -n command-line option. n - If auto-print is not disabled, print the pattern space, then, regardless, replace the pattern space with the next line of input.


2 Answers

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' 
like image 92
Johnsyweb Avatar answered Sep 23 '22 15:09

Johnsyweb


You can use wc -l to figure out the total # of lines.

You can then combine head and tail to get at the range you want. Let's assume the log is 40,000 lines, you want the last 1562 lines, then of those you want the first 838. So:

tail -1562 MyHugeLogFile.log | head -838 | .... 

Or there's probably an easier way using sed or awk.

like image 21
David Avatar answered Sep 21 '22 15:09

David