0

I have the following command to insert a string variable to a file after line 1:

sed '1a\"$str_builder"' certs.json > certs-new.json

I have used double quotes around the variable as the answers in other similar posts have said to do (so don't mark as duplicate pls), but it still doesn't work and just inserts the string $str_builder

The file certs.json looked like:

[
    {
        "url" : "www.google.com",
    .
    .

And certs-new.json looks like:

[
$str_builder
    {
        "url" : "www.google.com",
    .
    .
6
  • Use double-quotes to expand shell variables Commented Apr 17, 2018 at 10:42
  • @Inian read post clearly please Commented Apr 17, 2018 at 11:05
  • What value you are storing in variable str_builder? Commented Apr 17, 2018 at 11:18
  • @AbhijitPritam just a string block to insert as a new JSON entry Commented Apr 17, 2018 at 11:24
  • is it like "something" : "value"? Commented Apr 17, 2018 at 11:24

3 Answers 3

1

Use double quotes around the whole thing and remove the backslash.

sed "1a$str_builder" certs.json > certs-new.json
Sign up to request clarification or add additional context in comments.

4 Comments

thanks for the suggestion but this doesn't work unfortunately, just writes $str_builder
Ah - if you're using sed to insert lines then you don't need the backslash character. Try without it: sed "1a$str_builder" certs.json > certs-new.json
Thanks this worked!! If you edit your original answer or write a new one I can accept it
Just one thing, it's removing the first backslash and so the first tab \t is coming through as just a t, I've tried changing it to \\t but it's still coming through as just t
0

Single quotes inhibit variable expansion. You need to use double quotes.

sed "1a$str_builder" certs.json > certs-new.json

If you want double quotes inside the outer quotes, the inner ones need to be escaped.

sed "1a\"$str_builder\"" certs.json > certs-new.json

You'd probably be better off using jq to modify your JSON data, though. It's a smarter tool designed expressly for JSON manipulation.

1 Comment

Thanks, but this also inserts a pair of " around the expanded variable. And thanks for the tip about jq!
0

I tried sed command in my system from the below two answer and got error sed: command garbled: hence quickly giving below solution without a sed command for the time being. Will check once get time how to fix it with sed.

 #first print the first line of file certs.json
 head -1 certs.json > certs-new.json
 #Now insert value of variable str_builder into certs-new.json as second line
 echo "$str_builder" >> certs-new.json

 #now insert all the lines except first line from certs.json to certs-new.json
 tail +2 certs.json >> certs-new.json

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.