views:

171

answers:

2

There are often times I'll grep -n whatev file to find what I'm looking for. Say the output is

1234: whatev 1
5555: whatev 2
6643: whatev 3

If I want to then just extract the lines between 1234 and 5555, is there a tool to do that? For static files I have a script that does wc -l of the file and then does the math to split it out with tail & head but that doesn't work out so well with log files that are constantly being written to.

A: 

grep is designed to find strings in an input stream. Chaining a series of tools together in a script is a very appropriate and "Unix-y" way of doing what you want.

What is the particular issue with "log files that are constantly being written to"? Your example doesn't really explain what those lines are supposed to be or why you want the range between 1234 and 5555.

Daniel DiPaolo
The beginning and the end of a particular series of events I'm interested in.
Mike
+2  A: 

Try using sed as mentioned on http://linuxcommando.blogspot.com/2008/03/using-sed-to-extract-lines-in-text-file.html. For example use

sed '2,4!d' somefile.txt

to print from the second line to the fourth line of somefile.txt. (And don't forget to check http://www.grymoire.com/Unix/Sed.html, sed is a wonderful tool.)

Scorchio