by JepZ · 8y ago · 0 comments
sed 's/a/b/' test.txt > test.txt
After that command the file is empty no matter what was in there before. Instead you have to use the -i flag like: sed 's/a/b/' -i test.txt
And different operating systems seem to behave differently:https://stackoverflow.com/questions/5171901/sed-command-find...
(It writes to a temporary file, and then atomically renames it to the final location. Whereas if sed expr src > dst is killed, dst may be corrupt. You'd need to handle temporary file management yourself if your script needs to be reliable.)
Anyway sed is so fast you should definitely check the output before overwriting stuff. Same goes for anything on the command line.
Get ZFS. Do snapshots.
That wouldn't really help in the case of redirecting sed to the same file.
$ echo "a" > test.txt
$ sed s/a/b/ test.txt
b
$ sed s/a/b/ test.txt > test.txt
$ cat test.txt
$I prefer having a way of reverting changes rather than something that gets in the way.
sed "s/root/toor/" /etc/passwd | grep -v joey | sponge /etc/passwd
https://joeyh.name/code/moreutils/