Rereading your question, I think you are just missing the -i
option to sed
. That is the option that makes sed
edit the file in place rather than printing the results to standard output.
Note that it is a very good idea to test your sed
command before using the -i
switch, as there is no "undo" command if you get it wrong.
So, for example, if you run sed 's/old/new/' myfile
, you will see the contents of myfile
printed out on your terminal with the first instance of old
on each line replaced with new
. If you run sed -i 's/old/new/' myfile
, you won't see anything printed out—myfile
will be (irrevocably) edited in place.
You can also use, for example, -i.bak
instead of -i
, which will cause the original file to be saved as myfile.bak
and the edited file to appear as myfile
. (You can define whatever extension you want instead of .bak
and it doesn't need to start with a dot, but .bak
is conventional to use for this purpose.)
Quotes around your space-containing filename is a separate issue and the answer to that is simple—use double quotes to get variable expansion without word splitting. So if you have a variable filenamevariable="My file name which contains spaces"
, you need to refer to it like so: sed 's/old/new/' "$filenamevariable"
Some other recommended reading:
"$FILE"
or'$FILE'
? – steeldriver Dec 21 '15 at 06:53$SAMPLE
variable contains any/
characters (or&
or\
). And, you might want to try embedding the wholesed
command in an-exec
operator to thefind
command rather than looping by hand. – Wildcard Dec 21 '15 at 06:56for
loop to iterate through files, actually. The big anti-pattern is using afor
loop to iterate through lines of a file; see the question I linked to from my answer about shell loops for processing text. – Wildcard Dec 21 '15 at 16:42