When working with large log files, a very common requirement is to extract a piece of log that's interesting. Most log files are actually text files, therefore it should be quite easy to do this, as long as you know the start line and the end line.
The following bash script will print to the standard output the content of the given text file, from the given start line to the end line.
#!/bin/bash if [ "$#" -ne "3" ]; then echo "Not enough parameters." echo "${0} <filename> startline endline" exit; fi # Extract the fragment between the two line numbers head -"$3" "$1" | tail -"$(($3-$2))"
As you can see, the magic happens on the last line:
head -"$3" "$1" | tail -"$(($3-$2))"
The first command, head, returns the first $3 lines from the given input file; its result is piped to tail, which returns the last $(($3-$2)) lines - $(($3-$2)) actually means the arithmetic difference between the third parameter and the second one. Simple?
Comments and suggestions are always welcomed.