Good evening,I would like to filter a file's content with some piped commands and then write the result back to the same file. I know, I can't do that the way I wrote it. Hold on This is the piece of bash script I have.grep '^[a-zA-Z.:]' $filepath \    | sed -r '/^(rm|cd)/d' \    | uniq -u \    > $filepathSo I thought I could succeed in, using process substitution instead. I then wrote:grep '^[a-zA-Z.:]' < <(cat $filepath) | This did not solve anything either. I expected process substitution to  save  my input file content somewhere, like in a temporary file. It seams I haven't understood process substitution either.I read threads about inplace edition but these articles highlighted special options of some binaries like sed -i or sort -o but I need a general solution (I mean it has to suit any piped commands).So first, why 'pipes standard way' cannot do this, what's happening underneath ? :/ And how should I solve my issue ? Could someone please explain me what is this all about ?Thank you.
Processing a single file as both input and output throughout pipes
bash;pipe;command substitution;process substitution
As has been mentioned, sponge from moreutils is great.I use this script to emulate to avoid the moreutils dependecy:#!/bin/sh -e#Soak up input and tee it to argumentsst=0; tmpf=tmpf=`mktemp` && exec 3<>$tmpf || st=$?rm -f $tmpf #remove it even if exec failed; noop if mktemp failed[ $st = 0 ] || exit $stcat >&3</dev/fd/3 tee $@ >/dev/nullYou can use it like so:grep '^[a-zA-Z.:]' $filepath \| sed -r '/^(rm|cd)/d' \| uniq -u | sponge $filepath You can't do this with simple output redirection because redirections take place before the commands are started and an output redirection truncates the output file.In other words, by the time grep (the first simple command of the pipeline) starts, the last redirection has already truncated the input/output file.There aren't really any standard UNIX utilities that do true in-place editing, as far as I know. sed -i only emulates it with a temporary file. I guess the reason is that true inplace filtering can easily corrupt the file if a pipeline step fails.As far as what's going on underneath -- both | and <() use system pipes which take pass IO a buffer at a time. The mechanism doesn't create temporary files (not real (filesystem) files anyway) and it tries to avoid holding the whole input in memory at a time.   