windows - sed 不能在 Windows 的脚本文件中工作

标签 windows sed

我曾经写过这样一个简单的 sed 命令

s/==/EQU/

当我在命令行中运行它时:

sed 's/==/EQU' filename

它运行良好,将“==”替换为“EQU”,但是当我将命令写入名为 replace.sed 的脚本文件时,以这种方式运行它:

sed -f replace.sed filename

有错误,说是

sed: file replace.sed line 1: unknwon option to 's'

我想问的是,我的脚本文件replace.sed在windows下运行有问题吗?

最佳答案

未知选项几乎总是在尾随 / 之后的流氓字符(顺便说一句,您的命令行版本中缺少它,因此它应该提示未终止的命令)。

再看看你的replace.sed。你可能在末尾有一个有趣的字符,如果你忘记删除它,它可能包括 ',甚至是 CTRL-M DOS 风格的行结束,虽然 CygWin似乎可以很好地处理这个问题 - 你没有指定 哪个 sed 你正在使用(这可能有帮助)。


好的,根据您的编辑,看起来我的建议之一是正确的:-) 由于 CR/LF 行尾,您在行尾有 CTRL-M :

At the end of each line in the *.sed file, there was a 'CR\LF' pair, and that the problem, but you cannot see it by default, I use notepad to delete them manually and fix the problem. But I have not find a way to delete it automatically or do not contain the 'new-line' style while edit a new text file in windows.

您可能想要使用更强大的编辑器,如 Notepad++ 或 gVim(我最喜欢的),但事实上,您确实有一个可以摆脱这些字符的工具:-)它叫做 sed

sed 's/\015//g' replace.sed >replace2.sed

应该从文件中删除所有 CR 字符,并为您提供可用于实际工作的 replace2.sed

关于windows - sed 不能在 Windows 的脚本文件中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4066498/

相关文章:

windows - 使用 Docker 容器连接拒绝本地主机 (Windows)

css - 用于从文件中查找、转换和替换值的 Unix 脚本

linux - 仅当文件不在文件中时才向文件添加行

c - 如何以编程方式更改 Windows 音频设备的友好名称?

windows - 如何在子进程中启动崩溃(很少)的应用程序

c++ - 应该如何从回调函数更新/返回值?

windows - CMake 安装不在 Windows 上安装库

linux - 在 SED Linux 中查找和替换 Define(Key,Value)

linux - Bash 脚本以 json 格式打印输出

c - 在 C 程序中使用 sed 等价物