linux - 执行sed命令时出错

标签 linux bash ubuntu sed

我有一个文件 exa1.txt其中包含以下文本:

wget -O backward-cpp-$VERSION.tar.gz https://github.com/bombela/backward-cpp/archive/v$VERSION.tar.gz
tar xf backward-cpp-$VERSION.tar.gz
testString

我需要使用 linux sed 附加/插入以下行命令

sed -i '/uctx->uc_mcontext.regs->nip/a \#elif defined(__s390x__)\n\                error_addr = reinterpret_cast<void*>(uctx->uc_mcontext.psw.addr);' backward-cpp-$VERSION/backward.hpp

我尝试了以下方法

选项1

sed -i  '/tar xf backward-cpp-$VERSION.tar.gz/a\
sed -i '/uctx\->uc_mcontext.regs\->nip/a \\#elif defined\(__s390x__\)\n\ error_addr = reinterpret_cast<void*>(uctx->uc_mcontext.psw.addr);' backward-cpp-$VERSION/backward.hpp' exa1.txt  /$BUILDDIR/envoy/ci/build_container/build_recipes/backward.sh

选项2

sed -i -e 's/testString/sed -i '/uctx->uc_mcontext.regs->nip/a \\#elif defined\(__s390x__\)\\n\\                error_addr = reinterpret_cast<void*>\(uctx->uc_mcontext.psw.addr\);' backward-cpp-$VERSION/backward.hpp/g' exa1.txt

但我收到此错误

-bash: nip/a: No such file or directory
-bash:  backward-cpp-$VERSION/backward.hpp/g: No such file or directory

最佳答案

如果您只想在文件末尾添加一些行,则无需使用 sed:

$ cat exa1.txt - > output <<EOF
some lines you want to insert
go here
EOF

在命令中,-指的是标准输入,即<<EOF之间的heredoc内容和EOF .

现在output包含 con cat第一个文件的内容以及 <<EOF 之间放置的任何行和EOF 。记得逃跑$如果您不希望它们被 shell 扩展,否则在 Bash 等 shell 上,您可以使用 <<'EOF'这样一切都按字面意思处理。

要实现“就地”编辑,只需使用mv覆盖原始文件即可。 .

或者,只需创建另一个脚本,首先运行 exa1.txt 中的脚本然后是任何其他命令。

关于linux - 执行sed命令时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48882201/

相关文章:

ubuntu - 在 Ubuntu 18.04 LTS WSL 中更改目录

linux - 如何将 Rundeck key 存储传递给脚本

php 使用 system()/shell_exec()/exec() 运行终端应用程序,但只显示一半的输出

bash - 如何使用 bash 脚本在一个文件中找到行而不在另一个文件中找到行?

linux - 在 Ubuntu 14.04 中使用 Bash 作为 eclipse 外部工具和 node.js

c - 如何在 Ubuntu 上使用 C 文件 I/O 创建文件

PHP无法在ubuntu中连接到postgreSQL

linux - 在 perl 中截图

linux - 如何在 USB 大容量存储模式下通过手机列出连接到 Linux 的 SD 卡的内容?

c - ***没有制定目标 '../../../Makefile.include' 的规则。停止