You can use a bash read/write file descriptor to open your file (andto overwrite it in-situ), then sed and truncate ... but of course, don't ever allow your changes to be larger than the amount of data read so far.
Here is the script
echo "going abc" >junk # just a test file
echo "going def" >>junk
echo "# ORIGINAL file";cat junk |tee >( wc=($(wc)); echo "# ${wc[0]} lines, ${wc[2]} bytes" ;echo )
#
exec 3<> junk # Assign file to fd 3, and open it r/w
<junk sed -e "s/going //" |tee >(wc -c >newsize) >&3
# TODO: dedicated wait logic for the newsize process to finish, as it runs asynchronously... see: http://unix.stackexchange.com/questions/10623/a-tee-process-is-truncating-its-stdout-when-writing-a-file
exec 3>&- # close fd 3.
newsize=$(cat newsize)
echo "# MODIFIED file";cat junk |tee >( wc=($(wc)); echo "# ${wc[0]} lines, ${wc[2]} bytes" ;echo ) cat junk
#
truncate -s $newsize junk
echo "# NEW (truncated) file";cat junk |tee >( wc=($(wc)); echo "# ${wc[0]} lines, ${wc[2]} bytes" ;echo ) cat junk
Here is the test output
# ORIGINAL file
going abc
going def
# 2 lines, 20 bytes
# MODIFIED file (before truncating)
abc
def
c
going def
# 4 lines, 20 bytes
# NEW (truncated) file
abc
def
# 2 lines, 8 bytes