Basically I want to take as input text from a file, remove a line from that file, and send the output back to the same file. Something along these lines if that makes it any clearer.
grep -v 'seg[0-9]\{1,\}\.[0-9]\{1\}' file_name > file_name
however, when I do this I end up with a blank file. Any thoughts?
I usually use the tee program to do this:
It creates and removes a tempfile by itself.
You cannot do that because bash processes the redirections first, then executes the command. So by the time grep looks at file_name, it is already empty. You can use a temporary file though.
like that, consider using
mktemp
to create the tmpfile but note that it's not POSIX.try this simple one
Your file will not be blank this time :) and your output is also printed to your terminal.
You can do that using process-substitution.
It's a bit of a hack though as bash opens all pipes asynchronously and we have to work around that using
sleep
so YMMV.In your example:
>(sleep 1 && cat > file_name)
creates a temporary file that receives the output from grepsleep 1
delays for a second to give grep time to parse the input filecat > file_name
writes the outputUse sponge for this kind of tasks. Its part of moreutils.
Try this command:
Use sed instead: