Easy ex editor command

I've this command in a script which edits the file ...

bash$ cat temp_file.txt
THREAD #2 2 Running
bash$ (echo "s/THREAD #2/d"; echo 'wq') | ex -s temp_file.txt
bash$ cat temp_file.txt
THREAD #2 2 Running

If i've more than 1 line it easily deletes the line, but if it is the last line left in the file it simply does not delete>?

not able to figure out ... please help

Assuming this is vim... you need to do something like:

(echo '%g/THREAD #2/d'; echo 'wq') | ex -s temp_file.txt
This line quoted actually changes the string to a character "d" rather than deleting the line.

(echo "s/THREAD #2/d"; echo 'wq') | ex -s temp_file.txt

This will actually delete the line containing the first occurance of the string.

(echo "/THREAD #2/d"; echo 'wq') | ex -s temp_file.txt

However if another process has the file open (check with "fuser") then the edit could be failing.

my bad ...

it was (echo "g/THREAD #2/d"; echo 'wq') | ex -s temp_file.txt

cjcox ... I tired adding % in front of g it would not work...

methyl...Your poposal also does not delete the line if its the finst and only line in the file

Thanks to both of you for replying .... Please feel free to try on ur terminal as it is only one line file and one command ...