-2

I have one file exa1.txt which contains the following text:

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

I need to append/insert following line using linux sed command

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

I tried the following

Option-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

Option-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

but I get this error

-bash: nip/a: No such file or directory
-bash:  backward-cpp-$VERSION/backward.hpp/g: No such file or directory
Anuj
  • 1
  • You're using apostrophes without escaping them. They should surround the sed-Command and MUST NOT be used inside without escaping. – Stefan M Feb 20 '18 at 09:58
  • Please tell me how to insert this line in any file at any place using sed command – Anuj Feb 20 '18 at 13:47
  • sed -i '/uctx->uc_mcontext.regs->nip/a \#elif defined(__s390x__)\n\ error_addr = reinterpret_cast(uctx->uc_mcontext.psw.addr);' backward-cpp-$VERSION/backward.hpp – Anuj Feb 20 '18 at 13:47

1 Answers1

0

If you just want to add some lines to the end of a file, then you have no need to use sed:

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

In the command, - refers to standard input, which is the contents of the heredoc between <<EOF and EOF.

Now output contains the concatenation of the contents of the first file, and any lines you put between <<EOF and EOF. Remember to escape $ if you don't want them to be expanded by the shell, otherwise on shells such as Bash, you can use <<'EOF' so that everything is treated literally.

To achieve an "in-place" edit, just overwrite the original file using mv.

Or, just create another script that first runs the script in exa1.txt and then whatever additional commands.

Tom Fenech
  • 72,334
  • 12
  • 107
  • 141
  • need to edit one file at runtime while executing jenkins build script. – Anuj Feb 20 '18 at 10:07
  • @Anuj I edited my answer – Tom Fenech Feb 20 '18 at 10:17
  • Can we achieve it through 'sed' command to insert multiple lines with special characters. In my scenario have to write jenkins build shell script where i have to edit lots of files at run time. Please help. – Anuj Feb 20 '18 at 12:38