ambevar-dotfiles/.scripts/cutlines

52 lines
978 B
Bash
Executable File

#!/bin/sh
_usage () {
cat<<EOF
Usage: ${1##*/} [OPTIONS] [FILES]
Cut out last 10 lines from stdin. If FILES are provided, cut files instead of
stdin and output to stdout.
Note: GNU 'head' has the '-n -10' option to do this. This program is only useful
for non-GNU systems.
Options:
-f: Cut out first 11 instead of the last. Like 'tail -n +11' with GNU tail (11
because the limit line is included).
-h: Show this help.
-i: Replace file content instead of writing to stdout.
-n N: Cut N lines instead of 10.
EOF
}
OPT_CUTFIRST=false
OPT_LINES=10
OPT_INPLACE=""
while getopts ":fhin:" opt; do
case $opt in
f)
OPT_CUTFIRST=true ;;
h)
_usage "$0"
exit 1 ;;
i)
OPT_INPLACE="-i" ;;
n)
OPT_LINES=$OPTARG ;;
\?)
_usage "$0"
exit 1 ;;
esac
done
shift $(($OPTIND - 1))
if $OPT_CUTFIRST; then
sed $OPT_INPLACE -n "1,${OPT_LINES}!p" "$@"
else
echo CUTLAST
sed $OPT_INPLACE -n ":begin ; 1,${OPT_LINES}!{P;N;D}; N; b begin" "$@"
fi