Skip to content

Instantly share code, notes, and snippets.

@William-Yeh
Created September 16, 2014 04:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save William-Yeh/6ae510e6e6e20efe1f41 to your computer and use it in GitHub Desktop.
Save William-Yeh/6ae510e6e6e20efe1f41 to your computer and use it in GitHub Desktop.
Extract a range of lines from stdin and print to stdout.
#!/bin/bash
#
# Extract a range of lines from stdin and print to stdout.
#
# Usage:
#
# head2tail.sh starting-line [number-of-lines]
#
# Example:
#
# - Extract lines 10--16 from input-file:
# $ cat input-file | head2tail.sh 10 6
#
# @see http://unix.stackexchange.com/questions/47407/cat-line-x-to-line-y-on-a-huge-file/47414#47414
#
STARTING_LINE=$1
NUMBER_OF_LINES=${2:-9876543210}
if [ $# -lt 1 ]; then
echo "Extract a range of lines from stdin and print to stdout.."
echo "Usage: head2tail.sh starting-line [number-of-lines]"
exit 1
fi
# < inputfile tail -n +"10" | head -n "6" > outputfile
tail -n +"${STARTING_LINE}" | head -n "${NUMBER_OF_LINES}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment