I was doing some hands-on with the Unix sed command. I was trying out the substitution and append command, in a file. But the difficulty is, I have to create an intermediate file, and then do mv to rename it to the original file.
Is there any way to do it at one shot in the same file?
[root@dhcppc0 practice]# sed '1i\
> Today is Sunday
> ' file1 > file1
[root@dhcppc0 practice]# cat file1
[root@dhcppc0 practice]#
The file is deleted!
[root@dhcppc0 practice]# sed 's/director/painter/' file1 > file1
[root@dhcppc0 practice]# cat file1
The file is deleted!
GNU sed knows an option
-iwhich does in-place edit of the given files.When doing an operation
file1 > file1what actually happens is, that the file is opened and truncated by the shell before the program (which gets it’s name as argument) comes around reading anything from it.Update:
sed’s man page states the following on the
-ioption (thanks Delan for mentioning it):