sed with multiple expression for in-place argument
Depending on the version of sed
on your system you may be able to do
sed -i 's/Some/any/; s/item/stuff/' file
You don't need the g
after the final slash in the s
command here, since you're only doing one replacement per line.
Alternatively:
sed -i -e 's/Some/any/' -e 's/item/stuff/' file
Or:
sed -i '
s/Some/any/
s/item/stuff/' file
The -i
option (a GNU extension now supported by a few other implementations though some need -i ''
instead) tells sed
to edit files in place; if there are characters immediately after the -i
then sed
makes a backup of the original file and uses those characters as the backup file's extension. Eg,
sed -i.bak 's/Some/any/; s/item/stuff/' file
or
sed -i'.bak' 's/Some/any/; s/item/stuff/' file
will modify file
, saving the original to file.bak
.
Of course, on a Unix (or Unix-like) system, we normally use '~' rather than '.bak', so
sed -i~ 's/Some/any/;s/item/stuff/' file
You can chain sed expressions together with ";"
%sed -i 's/Some/any/g;s/item/stuff/g' file1
%cat file1
anything 123 stuff1
anything 456 stuff2
anything 768 stuff3
anything 353 stuff4
Multiple expression using multiple -e
options:
sed -i.bk -e 's/Some/any/g' -e 's/item/stuff/g' file
or you can use just one:
sed -i.bk -e 's/Some/any/g;s/item/stuff/g' file
You should give an extension for backup file, since when some implementation of sed
, like OSX sed does not work with empty extension (You must use sed -i ''
to override the original files).