首页 > 运维 > linux运维 > 正文

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

看不見的法師
发布: 2025-06-26 10:46:15
原创
562人浏览过

linux系统上使用sed命令进行文件内字符串替换是非常顺畅的。例如,以下命令在linux上可以无缝工作:

sed -i "s/find/replace/g" file.txt
登录后复制

然而,在macOS上使用相同的命令时,可能会遇到错误。这是因为macOS和Linux在处理sed命令的-i选项上存在差异。具体来说,macOS使用的是BSD版本的sed,而Linux使用的是GNU版本的sed。

根据Stack Overflow上的一篇帖子《sed command with -i option (in-place editing) works fine on Ubuntu but not Mac》,我们了解到-i选项在两者之间处理方式的细微差别。在macOS上,-i选项需要一个后缀参数,即使你不希望创建备份文件,也必须提供一个空字符串作为后缀。例如:

sed -i "" "s/find/replace/g" file.txt
登录后复制

Linux版本的sed允许-i选项后跟一个可选的后缀,如果不提供后缀,则不会创建备份文件:

       -i[SUFFIX], --in-place[=SUFFIX]              edit files in place (makes backup if SUFFIX supplied)
登录后复制

而在macOS上,-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和macOS上都能正确使用sed -i选项,可以采用以下方法进行调整:

# 定义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的语法,可以考虑在macOS上安装gsed:

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

参考资料《sed command with -i option (in-place editing) works fine on Ubuntu but not Mac [duplicate]》

以上就是macOS:sed -i报错:sed: 1: “xxxxx“: extra characters at the end of p command的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号