How to delete line in the same file in unix ?

How to delete line in the same file in unix ?
Both commands,
/bin/echo "g/$patern/d\nwq!" | ex -s $file
(echo "g/$patern/d"; echo 'wq') | ex -s $filename
works fine if there are more than 1 line in file but if there is only one line then above delete command dosent works. Please help me !!

grep -v "$pattern" "$filename" >newfile

this will delete whole line where pattern is found...

the command is not working in my .sh script.
It gives error as syntax error at line number:'<<' unmatched..
Please help ...

sed -i "/$pattern/d" file

If your sed has the -i option.

perl -i -ne "print unless m/$pattern/" file

The -i option is supported in Perl since way back.