(Turn) sed under mac os to replace file content

In mac osx lion sed to replace the file content, use the command:

sed -i 's/oldstring/newstring/g' full-path-file

After execution, an error message is displayed. The error message is: "sed: 1: command a expects \ followed by text", but the same command can be executed successfully under centos. Use man to view the detailed description of the command parameters. The two systems have different requirements for the parameter "i".
In mac osx it is:

-i extension
  Edit files in-place, saving backups with the specified extension.  If a zero-length extension is given, no backup will be saved.
  It is not recommended to give a zero-length extension when in-place editing files, as you risk corruption or partial content in situations where disk space is exhausted, etc.
In centos it is:

-i[SUFFIX], --in-place[=SUFFIX]
  edit files in place (makes backup if extension supplied)


The purpose of the parameter "i" is to make substitutions directly in the file. In order to prevent catastrophic consequences from misoperation, sed can automatically back up files before replacing, provided a suffix is ​​provided. As can be seen from the detailed description of the parameter "i" above, backup is mandatory under mac osx (of course, an empty string can be used to cancel the backup), but it is optional under centos.

If you don't need backup files, you can use the following command to complete the replacement operation under mac osx:
sed -i '' 's/oldstring/newstring/g' full-path-file



example:
cd /tmp
echo "netingcn.com" > sed_test.txt
cat sed_test.txt
sed -i '' 's / netingcn / www \ .netingcn / g' sed_test.txt
cat sed_test.txt
rm -rf sed_test.txt
cd -


Go to: http://www.netingcn.com/mac-osx-sed-command-expects-followed-by-text.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326470770&siteId=291194637