Home > database >  Insert multiple lines into a file using shell script
Insert multiple lines into a file using shell script

Time:02-24

I want to insert multiple lines into a .json file after a specific pattern using shell script. Also need to consider to insert on the nth occurrence.

Let us consider my input file contents are: input.txt:

[ 
  {
    "a": "a",
    "b": "b"
  },
  {
    "c": "c",
    "d": "d"
  },
  {
    "e": "e",
    "f": "f"
  }
]

In my first case, I have to insert below two lines after the first occurrence of pattern "}," and in my second case the same two lines after the second occurrence of pattern "}," in the input.txt file

After inserting my file should change like this:

First case: Inserting x & y entries

[ 
   {
     "a": "a",
     "b": "b"
   },
   {
     "x": "x",
     "y": "y"
   },
   {
     "c": "c",
     "d": "d"
   },
   {
     "e": "e",
     "f": "f"
   }
]

Second case: Inserting m & n entries

[ 
   {
     "a": "a",
     "b": "b"
   },
   {
     "c": "c",
     "d": "d"
   },
   { 
     "m": "m",
     "n": "n"
   }
   {
     "e": "e",
     "f": "f"
   }
]

I am trying this with sed command in my shell scipt. But this always replaces in the first occurrence and not the nth occurrence.

sed -e '0,/},/{//a\  {\n    "x",\n    "y"\n  },' -e '}' input.txt file

Can any one help me?

CodePudding user response:

If you were using jq it'd as simple as

jq '.[:1]   [{"x":"x", "y":"y"}]   .[1:]' input.json

Demo

and

jq '.[:2]   [{"m":"m", "n":"n"}]   .[2:]' input.json

Demo

respectively.

CodePudding user response:

If it's something that you are planning to reuse you can do it in python.

#! /usr/bin/env python3

import json
import sys

j = json.load(sys.stdin)
j.insert(int(sys.argv[1]), json.loads(sys.argv[2]))
print(j)

and you can invoke it passing the position and object as parameters

./json-insert 1 '{"x": "x", "y": "y"}' < input.json

CodePudding user response:

 cat file.json|tr '\n' ' '| sed 's/},/},{"x":"x", "y":"y"},/2'|json_pp
[
   {
      "a" : "a",
      "b" : "b"
   },
   {
      "c" : "c",
      "d" : "d"
   },
   {
      "x" : "x",
      "y" : "y"
   },
   {
      "e" : "e",
      "f" : "f"
   }
]

#--------------------------------------------------------------

cat file.json|tr '\n' ' '| sed 's/},/},{"x":"x", "y":"y"},/2'|json_pp > tmp.json
mv tmp.json file.json

CodePudding user response:

This might work for you (GNU sed & Bash):

cat <<\! | sed -Ee '/\}/{x;s/^/x/;/^x{1}$/{x;r /dev/stdin' -e 'x};x}' file
  {
    "x": "x",
    "y": "y"
  },
!

or:

cat <<\! | sed -Ee '/\}/{x;s/^/x/;/^x{2}$/{x;r /dev/stdin' -e 'x};x}' file
  {
    "m": "m",
    "n": "n"
  },
!

Pipe strings to be inserted via /dev/stdin into a sed invocation, using a counter in the hold space to initiate the placement of the strings.

N.B. The r command has to be terminated by a newline, hence the reason for splitting the sed commands into two parts using the -e command option. The strings can be collected in a here-document and passed to /dev/stdin of the following pipe using the cat command.

  • Related