cat package.yaml > package.yaml
The above will truncate package.yaml
, and hence the file will be empty - cat
will have empty output.
How can I avoid truncating the file? Is there an alternative to using a temporary file? A variable maybe?
cat package.yaml > package.yaml
The above will truncate package.yaml
, and hence the file will be empty - cat
will have empty output.
How can I avoid truncating the file? Is there an alternative to using a temporary file? A variable maybe?
All utilities that allows for in-place editing of files does so by internally writing the result to a temporary file and then replacing the original file by this temporary file once the operation is complete.
Files can be modified in place, but you would only be able to overwrite existing data and/or extend the length of the file. This may be done using the dd
utility, for example:
$ cat file.txt
hello world
abc abc 123 123
$ cat insert.txt
hello!
$ dd if=insert.txt of=file.txt bs=1 seek=6 conv=notrunc
7+0 records in
7+0 records out
7 bytes transferred in 0.000 secs (30918 bytes/sec)
$ cat file.txt
hello hello!
bc abc 123 123
Here, we insert what's in insert.txt
into file.txt
by first seeking 6 bytes forward in the file (stepping past hello
and the space) and then modifying it. The conv=notrunc
stops the output file from being truncated at the end of the writing operation.
If if=insert.txt
was left out, it would have been possible to insert any text from the keyboard. Note that "inserting" is the wrong term here really. "Overwriting" may better describe what's happening (see the first character of the second line being overwritten by the newline at the end of insert.txt
).
You wouldn't want to edit a file this way though.
perl -pi
(also sed -i
, gawk -v inplace
) but is not the norm. See also the <>
redirection operator, $mapfile
in zsh
, moreutils sponge
, mmap()
...
– Stéphane Chazelas
Mar 04 '18 at 20:08
fallocate(FALLOC_FL_INSERT_RANGE)
on Linux for instance, though that's only in multiples of the filesystem block size).
– Stéphane Chazelas
Mar 04 '18 at 20:10
myvar=$(sed 's/my_string/new_string/' input) && echo "$myvar" > input
– jesse_b Feb 26 '18 at 18:22sponge
is ideal.The question seems to be a duplicate of the question earlier, which itself is a duplicate - both questions don't have descriptive titles however.
– Chris Stryczynski Feb 26 '18 at 18:32