Very often I do simple operations on a file, for example:
cat file1.txt|sed -r 's/^ *//'
to remove spaces at the beginning of the line. If I want to overwrite the file, the only way I know is:
cat file1.txt|sed -r 's/^ *//' > file2.txt mv file2.txt file1.txt
This is very ennoying, because I have to check if file2.txt exists, because I have to write two commands instead of one, and so on.
So I thought: is there a way to do a full file buffering inside a chain of piped commands? So i could write:
cat file1.txt| magicbuffercommand |sed -r 's/^ *//' > file1.txt
This command should buffer (until a maximum of bytes, of course) and wait for an EOF, and then should start to write to stdout.
Is there anything that can do such a thing?