delete line in a file

Hi all,
I would like to delete a line in file.txt which has a list of filename.
example:
file_a
file_b
file_c

lets say i have file_b in my directory, then i should delete file_b in file.txt and get output file.txt with only file_a and file_b.

Below is my script, my result is still
file_a
file_b
file_c
Anyone can help???
:confused: :confused: :confused:
#!/bin/bash
while read line
do
if [[ -r $line ]]
then
sed -e '/$line/d' $1 > $1.tmp
mv $1.tmp $1
else
echo "file not readable"
fi
done < $1

while read line

waits for your input each time. Is that what you need ? Or do you need a script which will check your file contents against the directory listing and then carry out your requirement.

vino

sed '/file_b/d' file.txt > new_file.txt

just wandering, is there a way to delete the line out of "file.txt" without having to redirect it to a "new_file.txt" , or do we have flush "file.txt" and copy "new_file.txt" into it?

  /bin/echo "/$line/d\\nwq!" | ex -s file.txt

yep that works. i was just curious
why does this work
/bin/echo "/$line/d\nwq!" | ex -s file.txt
but not this?
echo "/$line/d\nwq!" | ex -s file.txt

'echo' is a built-in in bash/ksh which has a different syntax than the /bin/echo

oh interesting. thanks

/bin/echo "/$line/d\nwq!" | ex -s file.txt

ive noticed that this only removes one instance of "$line",
how would you remove all instances of $line from the file?

/bin/echo "g/$line/d\nwq!" | ex -s file.txt

Hi all,
I changed my script to the below, and it works now......
Thanks!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! :smiley:
#!/bin/bash
while read line
do
if [[ -r $line ]]; then
/bin/echo "g/$line/d\nwq!" | ex -s "${1}"
else
echo "file not readable"
fi
done < "${1}"

***script provided by vgersh99.
***Special thanks to vgersh99!!!!!!!!!!!

how would u use this to remove every line that has the charater "*" in it?

what do you think?