Monday, January 21, 2008

Removing first and last 5 lines from a file - BASH newbie


This question was there in UNIX.com shell scripting forum, thought of sharing here.

Solution 1:

$ S=`awk 'END {print NR-4}' file.tmp`; sed -e '1,5 d' -e "$S,$ d" file.tmp > file.tmp.tmp ; mv file.tmp.tmp file.tmp

Solution2:
$ tot=`wc -l file.tmp|cut -f1 -d" "`
$ more +6 file.tmp | head -$(( tot-10 )) > file.tmp.tmp ; mv file.tmp.tmp file.tmp

No comments:

© Jadu Saikia www.UNIXCL.com