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

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

如下,执行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]》 ↩︎

内容概要:本文围绕可变桨叶四旋翼无人机的规范控制与点对点运动模拟展开,重点研究优化推力分配策略在翻转动作中的应用与性能比较。通过Matlab代码实现,构建了四旋翼动力学模型,并设计了多种控制算法以实现精确的姿态调整与轨迹跟踪。研究对比了不同推力分配方案在执行高机动性翻转动作时的稳定性、能耗效率与响应速度,旨在提升无人机在复杂飞行任务中的动态性能与控制精度。该仿真研究为无人机飞控系统的设计与优化提供了理论依据和技术支持。; 适合人群:具备一定自动控制理论基础和Matlab编程能力,从事无人机控制、飞行器动力学或机器人系统研究的科研人员及研究生。; 使用场景及目标:① 实现四旋翼无人机在三维空间中的精确点对点运动控制;② 对比分析不同推力分配策略在执行翻转等高难度动作时的控制效果与能耗表现,优化飞行性能;③ 为无人机自主飞行、特技飞行及复杂环境下的机动控制提供算法验证平台。; 阅读建议:此资源以Matlab仿真为核心,建议读者结合相关控制理论知识,深入理解代码实现细节,重点关注动力学建模、控制律设计与推力分配模块。在学习过程中,应动手调试参数,复现文中翻转动作的仿真结果,并尝试拓展至其他复杂飞行任务,以加深对无人机控制机理的理解。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

10km

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

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

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

打赏作者

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

抵扣说明:

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

余额充值