linux - sed 返回 "sed: command garbled"

标签 linux bash sed replace

我在 file.txt 中有这些数据:

1234-abca-dgdsf-kds-2;abc dfsfds 2
123-abcdegfs-sdsd;dsfdsf dfd f
12523-cvjbsvndv-dvd-dvdv;dsfdsfpage

我想替换“-”到“;”之间的字符串只有“;”,所以我得到:

1234;abc dfsfds 2 
123;dsfdsf dfd f 
12523;dsfdsfpage

我尝试了命令:

sed -e "s/-.*;/;" file.txt

但它给了我以下错误:

sed command garbled

为什么会这样?

最佳答案

sed 替换命令定义为 ( source ):

's/REGEXP/REPLACEMENT/[FLAGS]'

(substitute) Match the regular-expression against the content of the pattern space. If found, replace matched string with REPLACEMENT.

但是,你是说:

sed "s/-.*;/;"

即:

sed "s/REGEXP/REPLACEMENT"

因此在表达式末尾缺少“/”。只需添加它即可:

sed "s/-.*;/;/"
#            ^

关于linux - sed 返回 "sed: command garbled",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19388443/

相关文章:

linux - ssh sudo 以不同用户在远程 Linux 服务器上执行命令

regex - 用另一个字符替换正则表达式匹配中的所有字符?

linux - 仅为我自己禁用和重新启用地址空间布局随机化

bash - 从图像为 FFmpeg 生成调色板

linux - 生成文件名以在 shell globing 下进行正确的顺序排序

bash - 使用docker exec执行主机上存在的Shell脚本时出现问题

linux - 使用 sed 替换 bash 文件中的变量 - 输出滞后

regex - 查找并替换多对字符

linux - 如何在引号、单引号或反引号中使用通配符或任何其他特殊字符?

linux - 如何解决gcc [-Werror=format-security] in function call?