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