bogac
bogac

Reputation: 1

sed for inserting multiple lines in a specific place in text file

Even though I have checked the relevant questions about this, I could not find any solution. I think my problem is very simple, but I cant fix it lack of knowledge.

$statistics dscf parallel
$2e-ints_shell_statistics    file=DSCF-par-stat
$parallel_parameters
   maxtask=4000
   maxdisk=0
   dynamic_fraction=0.300000
$pardft
   memdiv=1

I want to add these multiple lines to a text file after when it sees "unix" word. So my code is something like this.

sed -i "/unix/a \\
$statistics dscf parallel \\
$2e-ints_shell_statistics    file=DSCF-par-stat \\
$parallel_parameters \\
   maxtask=4000 \\
   maxdisk=0 \\
   dynamic_fraction=0.300000 \\
$pardft \\
   memdiv=1 \\
" control

control is my output file. because of the $ sign the output lacks some parts of this insertion which looks like this.

$operating system unix
 dscf parallel
e-ints_shell_statistics    file=DSCF-par-stat

   maxtask=4000
   maxdisk=0
   dynamic_fraction=0.300000

   memdiv=1

I got a bit confused. I am open to any kind of suggestion. It does not have to be with "sed" command.

p.s: This is my first question here. :)

Upvotes: 0

Views: 442

Answers (3)

potong
potong

Reputation: 58578

This might work for you (GNU sed, cat & bash):

cat <<\! | sed -i '/unix/r /dev/stdin' file
$statistics dscf parallel
$2e-ints_shell_statistics    file=DSCF-par-stat
$parallel_parameters
   maxtask=4000
   maxdisk=0
   dynamic_fraction=0.300000
$pardft
   memdiv=1
!

Upvotes: -1

NeronLeVelu
NeronLeVelu

Reputation: 10039

sed -i '/unix/ a\
$statistics dscf parallel\
$2e-ints_shell_statistics    file=DSCF-par-stat\
$parallel_parameters\
   maxtask=4000\
   maxdisk=0\
   dynamic_fraction=0.300000\
$pardft\
   memdiv=1\
' control

use simple quote around your sed action and a simple \ at end of line to append

Upvotes: 1

user4453924
user4453924

Reputation:

with awk

awk '1;/unix/{print "$statistics dscf parallel\n\
$2e-ints_shell_statistics    file=DSCF-par-stat\n\
$parallel_parameters\n\
maxtask=4000\n\
maxdisk=0\n\
dynamic_fraction=0.300000\n\
$pardft\n\
memdiv=1"}' file

Upvotes: 1

Related Questions