Earlier, I described a way to replace all occurrences of a particular pattern with another in a file and write the change(s) back into that file. BBBart on #linuxpakistan on irc.freenode.net suggested another way of doing it that does not involve calling another process. It uses the “-i” switch to sed(1), something I did not know of owing to the particular manpage for sed(1) on my system not documenting it. Here’s the alternative:
ayaz$ sed -i bak 's/pattern/replace/g' file
Note, however, that I have GNU sed version 3.02 installed and it doesn’t recognise the “-i” switch. I guess this funtionality is introduced in latter versions of sed.
One Trackback
[...] replace, and commit back” 30 December, 2006 Posted by ayaz in Programming, Linux. trackback I showed the sed way to search for a pattern in a file, replace every occurrence of the patternwith something, and write all the changes back to the same file. I am, now, going to show you the Perl one-liner way of doing it. [...]