Re: How to cut the 1st and last 2 lines out of a file
- From: "Chris F.A. Johnson" <cfajohnson@xxxxxxxxx>
- Date: Fri, 30 Jun 2006 16:43:19 -0400
On 2006-06-30, jason.nesbitt@xxxxxxxxx wrote:
I'm looking for an efficient way to cut the 1st and last 2 lines out of
a file. I can get the first line out with "cat <filename> | tail +2l"
but after that I'm lost. Any suggestions?
This script will cut any number of lines from top and bottom of a
file.
# NAME: topntail - remove lines from top and bottom of a file
# USAGE: topntail [-b N] [-e N] [FILE ...]
b=
e=
while getopts b:e: opt
do
case $opt in
b) b=$OPTARG ;;
e) e=$OPTARG ;;
esac
done
shift $(( $OPTIND - 1 ))
case $b$e in
*[!0-9]*) exit 5 ;;
esac
if [ ${e:-1} -eq 0 ]
then
sed "1,${b:-1}d" "$@"
else
awk 'NR > b + e { print buf[ NR % e ] }
{ buf[ NR % e ] = $0 }' b=${b:-1} e=${e:-1} "$@"
fi
--
Chris F.A. Johnson, author <http://cfaj.freeshell.org>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
.
- References:
- How to cut the 1st and last 2 lines out of a file
- From: jason.nesbitt@xxxxxxxxx
- How to cut the 1st and last 2 lines out of a file
- Prev by Date: How to cut the 1st and last 2 lines out of a file
- Next by Date: Re: Test for an unset variable.
- Previous by thread: How to cut the 1st and last 2 lines out of a file
- Next by thread: Re: How to cut the 1st and last 2 lines out of a file
- Index(es):
Relevant Pages
|