macOS:sed -i报错:sed: 1: “xxxxx“: extra characters at the end of p command

37 篇文章 1 订阅
5 篇文章 0 订阅
本文介绍了在Linux和MacOS环境下使用sed进行字符串替换时遇到的差异,特别是-i选项的处理方式。通过比较两者的sed-i参数说明,提出了在不同系统间切换sed-i使用的技巧,并建议了在脚本中统一处理的方法。
摘要由CSDN通过智能技术生成

如下,执行sed对文件中的字符串进行替换,在Linux下是一点问题没有的。

sed -i "s/find/replace/g" file.txt

但是在macOS下却报错了

sed: 1: “file.txt”: extra characters at the end of p command

在stackoverflow上找到这个帖子《sed command with -i option (in-place editing) works fine on Ubuntu but not Mac》1,总算知道了原因:macOS与linux还是有差异的,这个问题就是macOS与linux之间差异造成的。

简言之,就是BSD/macOS 的sed和linux(GNU)下的sed 对于-i参数的处理有微小的差异。

-i 即inplace,即对文件原地修改,-i 后面可以指定一个后缀,比如(mscOS) -i .bak,或在linux下 -i.bak 即修改原文件并保存一个后缀为.bak的修改前的备份

如下是Linux下sed -i 参数说明

       -i[SUFFIX], --in-place[=SUFFIX]

              edit files in place (makes backup if SUFFIX supplied)

如下是macOS下sed -i 参数说明

     -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 situ-
             ations where disk space is exhausted, etc.

             Note that in-place editing with -I still takes place in a single
             continuous line address space covering all files, although each
             file preserves its individuality instead of forming one output
             stream.  The line counter is never reset between files, address
             ranges can span file boundaries, and the ``$'' address matches
             only the last line of the last file.  (See Sed Addresses.)  That
             can lead to unexpected results in many cases of in-place editing,
             where using -i is desired.

     -i extension
             Edit files in-place similarly to -I, but treat each file indepen-
             dently from other files.  In particular, line numbers in each
             file start at 1, the ``$'' address matches the last line of the
             current file, and address ranges are limited to the current file.
             (See Sed Addresses.)  The net result is as though each file were
             edited by a separate sed instance.

上面的说明可以看出区别Linux下-i 参数后面的[SUFFIX]是可选的(且与-i之间没有空格),如果不指定就不会备份
而macOS下-i参数后面的extension(扩展名,后缀)是必填参数(且与-i之间要有空格隔开),如果不想指定备份文件怎么办?必须跟一个空字符串,也就是-i ""
所以回到前面的那个例子,在macOS下就应该这么写

sed -i "" "s/find/replace/g" file.txt

如果你的脚本中很多地方都要用到sed -i 原地修改,而又希望在Linux和macOS下都能正常使用,推荐如下方式做一个替换:

# 定义sed -i 参数(数组)
# Default case for Linux sed, just use "-i"
sedi=(-i)
case "$(uname)" in
  # For macOS, use two parameters
  Darwin*) sedi=(-i "")
esac	

########

sed "${sedi[@]}" "s/find/replace/g" file.txt

如果你还是希望使用GNU sed 语法,可以参考下面的解决办法安装gsed
在这里插入图片描述

参考资料


  1. 《sed command with -i option (in-place editing) works fine on Ubuntu but not Mac [duplicate]》 ↩︎

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

10km

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值