How do I process a file or multiple files in place with bash?
So: read from file x
, do some processing (e.g. search-replace) and write to file x
.
I know that with sed
you can do: sed -i "" "s/original/replacement/g"
, but sometimes sed doesn't cut it and I need a different tool, which lacks an -i ""
-like option.
I recently discovered all by myself that I could do the following:
( BUFFER="`cat FILENAME`"; echo "$BUFFER" > FILENAME )
which uses an environment variable to store the contents of the file. For very large files this is probably not efficient and environment variables might be limited in capacity.
Is there a different way?
I also read this answer, but I'm looking for a bash solution. Open source tools that somehow fit the task perfectly are also welcome.
You can store the output of the command in a temp file, and then move the temp file over the original. Example: