Click to See Complete Forum and Search --> : scripting 303


DaCrusierI
07-16-2002, 01:43 PM
As I contiue my scripting learnings ...
Does anyone know of a command/program that can delete specific lines in a text file (i.e. lines 3 - 10)?

dchidelf
07-16-2002, 03:08 PM
Depending on your version of ex...

echo "3,10d\nwq" | ex -s -c - filename

should do it...
ex is the line editor behind vi, so a ton of things that work in vi work in ex...

Here is a ksh script that changes itself as it runs, using ex.


# This script returns 0 through n-1, where n is given as $1

LSRT=`echo "/[L]OOP/#\nq!" | ex -s -c - $0 | awk '{print $1+1}'`
LEND=`echo "/[P]OOL/#\nq!" | ex -s -c - $0 | awk '{print $1-1}'`
END=$((`wc -l < $0` + 1))

#LOOP
if [ "$VAL" -lt "$1" ]
then echo "${LSRT},${LEND}co\$\nwq"
printf "%d\n" $VAL > "/dev/tty"
else echo "\$a\n\n.\n${END},\$d\nwq"
fi | ex -s -c - $0
VAL=$(($VAL + 1))
#POOL

DaCrusierI
07-16-2002, 04:23 PM
Wow, that's ingenious ... well, close enough anyway. Thanks for the help.

furrycat
07-16-2002, 09:07 PM
sed 3,10d filename