论mac使用sed修改文件的正确姿势

业务变得复杂了,相关的代码目录也需要修改名字。 以前都是直接在centos开发机进行开发,所以没有遇到sed的问题。

文章写的不是很严谨,欢迎来喷,另外该文后续有更新的,请到原文地址查看更新。http://xiaorui.cc/2016/01/14/%E8%AE%BAmac%E4%BD%BF%E7%94%A8sed%E4%BF%AE%E6%94%B9%E6%96%87%E4%BB%B6%E7%9A%84%E6%AD%A3%E7%A1%AE%E5%A7%BF%E5%8A%BF/


sed问题如下.

#blog: xiaorui.cc

[ruifengyun@ct ec_master (master ✗)]$ sed -i "s/addr/ec_addr/g" lib/log.py
sed: 1: "lib/log.py": extra characters at the end of l command

解决方法:

man sed

    -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.

在mac sed help给出的一些例子中发现 sed  -i 需要带一个字符串,用来备份源文件,这个字符串会加在源文件名后面组成备份的文件名。
如果这个字符串长度为0,就是说是个空串,那么不备份。

#blog: xiaorui.cc

sed -i "_nima" 's/xiaorui/python/g' example.txt  

这样sed不仅会修改文件,并且会生成一个example.txt_nima的备份文件。

如果不想备份修改前的内容,可以直接给个空参数, sed -i  “” xxx.grovvy

#blog: xiaorui.cc

sed -i "" 's/xiaorui/python/g' example.txt  

另外说下,在mac下使用sed批量修改文件内容时遇到的问题.

上面说的是单个文件内容修改,如果想要批量递归文件修改的化,需要在前面加上find 或grep过滤文件.

[ruifengyun@devops ec_master (master ✗)]find ec_handler -name "*.py"|xargs sed -i "s/ec_handler/feed_handler/g"
sed: 1: "ec_handler/__init__.py": invalid command code e

[ruifengyun@devops ec_master (master ✗)] grep -rl "ec_handler" |xargs sed -i "s/ec_handler/feed_handler/g"
grep: warning: recursive search of stdin

正确利用sed批量修改文件的姿势是…  

find ec_handler -name "*.py"|xargs sed -i '' 's/ec_handler/feed_handler/g'

总之,一定要在mac sed加上备份参数,或后缀字符串,或空. 

如果你就是想用linux的sed, 可以这么安装gsed. 不建议你把gsed直接别名为sed, 有些外部进程会调用sed进行数据修改的,比如sublime.

sudo brew install gnu-sed

[ruifengyun@devops ec_master (master ✗)]$ gsed
Usage: /usr/local/homebrew/Cellar/gnu-sed/4.2.2/bin/gsed [OPTION]… {script-only-if-no-other-script} [input-file]…

  -n, –quiet, –silent
                 suppress automatic printing of pattern space
  -e script, –expression=script
                 add the script to the commands to be executed
  -f script-file, –file=script-file
                 add the contents of script-file to the commands to be executed
  –follow-symlinks
                 follow symlinks when processing in place
  -i[SUFFIX], –in-place[=SUFFIX]
                 edit files in place (makes backup if SUFFIX supplied)
  -l N, –line-length=N
                 specify the desired line-wrap length for the `l’ command
  –posix
                 disable all GNU extensions.
  -r, –regexp-extended
                 use extended regular expressions in the script.
  -s, –separate
                 consider files as separate rather than as a single continuous
                 long stream.
  -u, –unbuffered
                 load minimal amounts of data from the input files and flush
                 the output buffers more often
  -z, –null-data
                 separate lines by NUL characters
      –help     display this help and exit
      –version  output version information and exit

If no -e, –expression, -f, or –file option is given, then the first
non-option argument is taken as the sed script to interpret.  All
remaining arguments are names of input files; if no input files are
specified, then the standard input is read.

GNU sed home page: <http://www.gnu.org/software/sed/>.
General help using GNU software: <http://www.gnu.org/gethelp/>.

mac osx是个基于freebsd内核开发的系统,可能就因为这个,很多在linux特别流畅的shell命令,都不能流畅,痛快的使用起来.


大家觉得文章对你有些作用! 如果想赏钱,可以用微信扫描下面的二维码,感谢!
另外再次标注博客原地址  xiaorui.cc

发表评论

电子邮件地址不会被公开。 必填项已用*标注