How to use sed to replace the a string in the same file using sed?

How do i replace a string using sed into the same file without creating a intermediate file?

with the -i option (if available)

sed -i 's/ToBeReplaced/ReplacementString/'

Hi Frans!

Tried that already. The "-i" is not a valid option in my Solaris machine.

No way without intermediate file. :frowning:
But perl has something like

perl -pi 's/ToBeReplaced/ReplacementString/'

Please excuse the pedantism ahead. :wink:

It depends on what exactly you mean by an intermediate file. If you simply mean that you don't want to manually create a backup copy to work with, then, sure, if available, sed -i or perl -pi will do the job. However, if you must edit a file in place, truly in place, without consuming an inode, you should probably use ed.

To substitute every occurrence of a string in a file:

printf '%%s/original/replacement/g\nw\nq\n' | ed -s file

Note the inode change after each command except ed:

$ echo editor > f; cat f;  ls -i f
editor
4636174 f
$ sed -i -e 's/.*/sed -i/' f; cat f; ls -i f
sed -i
4636175 f
$ perl -pi -e 's/.*/perl -pi/' f; cat f; ls -i f
perl -pi
4636176 f
$ printf 's/.*/ed/\nw\nq\n' | ed -s f; cat f; ls -i f
ed
4636176 f

On a related note, if there is no strict in-place requirement, you can have fun with open descriptors:

$ (rm f; sed 's/.*/open descriptor fun/' > f) < f
$ cat f; ls -i f
open descriptor fun
4636178 f

This works because the unlink system call used by rm will not delete a file's contents if any process has an open file descriptor pointing to it; it will remove the file's entry from its directory, but the data is still available so long as the descriptor is open. In the code above, the subshell holds an open file descriptor on "f" (thanks to the input redirection), so when rm unlinks "f", while the data is no longer reachable via the name "f", it is still available via standard input to all processes created by the subshell. When sed runs, it reads the data formerly known as "f", but since "f" is no longer linked in the directory, redirecting sed's standard output to "f" clobbers nothing. Refer to unlink for more info.

It goes without saying that, while cool, the unlink/open-descriptor trick is vulnerable to system failure. If the system crashes at just the right moment, it's possible for the filesystem to not have any link to any version of the data. But, hey, you only live once, right? :wink:

Regards,
Alister

Thanks, Alister and Frans. The perl -pi did the job for me.

Alister - I have one question.. What is the advantage / necessity to preserve the same inode?

Unless your filesystem has run out of inodes, there is no advantage nor necessity. I was using the inode number to demonstrate that the file's identity (it's inode/serial number) is changed when sed/perl edits it (even if the name, in the end, is unchanged), in case your request strictly required in-place editing. It did not, therefore most of what I said has no relevance to your situation (though it may still be interesting to know ;)).

Cheers,
Alister

Thanks a lot for all the info !