Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google site




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #7 (permalink)  
Old 03-10-2007
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,557
Quote:
Originally Posted by ganesh123
Hello friends,

I want to remove 3 header lines and 4 trailer lines,

I am using following , is it correct ?


Code:

sed '1,3d';'4,$ d' filename

Is it correct? Run it and see.

To remove an arbitrary number of lines from both hte top and bottom of a file, use my topntail command:


Code:
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