Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8383949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:17:45+00:00 2026-06-09T17:17:45+00:00

I have been trying quite a few ways with no luck. I have a

  • 0

I have been trying quite a few ways with no luck. I have a file named test.txt that has some lorem ipsum and the text [staging: production] I simply want to add a few lines that I have saved in a variable in before it.

If you could explain where I have gone wrong with any of the below it would be much appreciated!

#!/bin/bash

test="lala\
kjdsh"

sed '/^#$/{N; /[staging: production]/ i \
<Location /cgis> \
</Location>\

}' ./test.txt


sed -i -e 's/\[staging\: production\]/\$test/g' ./test.txt
#sed -i 's/Lorem/beautiful/g' test.txt

#awk -v data=$test '{A[NR]=$0}/\[staging\: production\]/{ print data }' test.txt > testfile.txt

#read -a text <<<$(cat test.txt)
#echo ${#text[@]}
#for i in ${text[@]};
#do
#   echo -n $i;
#   sleep .2;
#done

#ed -s test.txt <<< $'/\[staging\: production\]/s/lalalala/g\nw'

#awk -v data=$test '/\(/\[staging\: production\]\)/ { print data }' test.txt > testfile.txt

# && mv testfile.txt test.txt

#sed -i -e '/\(\[staging\: production\]\)/r/$test\1/g' test.txt

#sed "/\(\[staging\: production\]\)/s//$test\1/g" test.txt
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T17:17:46+00:00Added an answer on June 9, 2026 at 5:17 pm
    sed -i -e 's/\[staging\: production\]/\$test/g' ./test.txt
    

    won’t work because inside singe quotes BASH will not expand \$test.
    Therefore you don’t need to escape the $.

    If you want to substitute with the contents of the variable $test do:

    sed -i -e 's/\[staging: production\]/'$test'/g' ./test.txt
    

    You also do not need to escape :

    To insert before your pattern works for me this way:

    sed -i -e '/\[staging: production\]/ i '$test'' ./test.txt
    

    However to preserve the linebreak inside the variable I needed to define:

    test="lala\nkjdsh"
    

    Please note the \n to encode the linebreak.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have been trying to encrypt an xml file to a string so that I
I have been trying my hand at php for quite some time but dont
We have a set of views (quite a few) that we're trying to implement
I have been trying for quite a while to check if submitted links are
I have been trying to figure this out for quite a while, but what
I have been trying to extract e-mail addresses from the following code for quite
I have been trying to setup git for our web development team unsuccessfully. Some
I've been having quite a few problems trying to use Microsoft.SqlServer.Types.SqlGeography . I know
Ive been trying to get this solved for quite a few hours, but I
I have been developing ASP.Net applications for quite a few years, and I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.