Click to See Complete Forum and Search --> : grep and replace within a file


Dave2001
01-20-2003, 02:26 PM
Hi Guys !

Iam kinda stuck with a problem .

For a script i need to read the first line out of a file and after it has been read - delete the entry.
then i need to go through the file again and read the next entry - and delete it again - till the file is empty .

the file contains several PIDs which i then want the user to give the option to kill

thing is i need to know how to make grep stop after the first match and then delete the entry . I have read it might work with awk - but i dont know how ...


Thanx already !!

The Kooman
01-20-2003, 11:39 PM
Hi Dave,

Why're you using grep if you want to read a line at a time??

I think you can try something like this ...


#!/bin/bash
#
# invoke with filename as first argument to the script

fname="$1"

while [ `stat -c "%s" "$fname"` -gt 0 ]
do
# you might want to catch the "head" instead of just displaying it.
head -n 1 $fname
set `wc -l $fname`
lines=`expr $1 - 1`
tail -n "$lines" $fname > tmpfile
mv tmpfile "$fname"
done


HTH!

Dave2001
01-21-2003, 01:37 AM
Thanks !

I will try that today and see if it fits in with my script !

Dave2001
01-21-2003, 01:20 PM
After a bit of modification for my script it worked like a charm !!
Thanx a lot !!