1

I want to write a script inside a file using bash, this script is generated automatically, lets say i have a file named some.sh with the following content :

{
   soemthings;

   soemthings_other;
}

i want to add script to some.sh to become like this

{
   soemthings;

   # the script 
   {
      hello there
      im the new script
      and multilines 
   }

   soemthings_other;
}

is it possible to do this using sed ?

1
  • What's the criteria for where to add the block of text? At the first blank line? At a specific line number? Before/after/between some specific strings? Something else? Is the block of text to be added stored in a file? A variable? Something else? Are there any specific characters it cannot contain? Basically - provide a little information about your requirements. Commented Jan 28, 2016 at 23:46

1 Answer 1

3

Create a file called script.tmp like this:

   # the script
   {
      hello there
      im the new script
      and multilines
   }

Then use this sed to insert this into some.sh:

sed -i.bak '/soemthings;/r script.tmp' some.sh

cat some.sh
{
   soemthings;
   # the script
   {
      hello there
      im the new script
      and multilines
   }

   soemthings_other;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.