1

I think sed should be the command to do this, but I haven't figured out the proper command yet.

My json file looks like this :

{"LAST_MODIFIED_BY":"david","LAST_MODIFIED_DATE":"2018-06-26 12:02:03.0","CLASS_NAME":"/SC/Trade/HTS_CA/1234abcd","DECISION":"AGREE","TASK_TYPE_ID":"abcd1234-832b-43b6-afa6-361253ffe1d5","NAME":"something"}
{"LAST_MODIFIED_BY":"sarah","LAST_MODIFIED_DATE":"2018-08-26 12:02:03.0","CLASS_NAME":"/SC/Import/HTS_US/9876abcd","DECISION":"DISAGREE","TASK_TYPE_ID":"abcd1234-832b-43b6-afa6-361253ffe1d5","NAME":"nicename"}
... more rows to follow

what I wanted to achieve is a json document with below contents:

{"index":{}}
{"LAST_MODIFIED_BY":"david","LAST_MODIFIED_DATE":"2018-06-26 12:02:03.0","CLASS_NAME":"/SC/Trade/HTS_CA/1234abcd","DECISION":"AGREE","TASK_TYPE_ID":"abcd1234-832b-43b6-afa6-361253ffe1d5","NAME":"something"}
{"index":{}}
{"LAST_MODIFIED_BY":"sarah","LAST_MODIFIED_DATE":"2018-08-26 12:02:03.0","CLASS_NAME":"/SC/Import/HTS_US/9876abcd","DECISION":"DISAGREE","TASK_TYPE_ID":"abcd1234-832b-43b6-afa6-361253ffe1d5","NAME":"nicename"}
... more rows to follow

so that I could run bulk load API against Elasticsearch.

The closest one is this one: Elasticsearch Bulk JSON Data, but it split my json file into broken items instead of my desired format.

Any ideas how I can achieve this would be greatly appreciated!

peak
  • 105,803
  • 17
  • 152
  • 177
Fisher Coder
  • 3,278
  • 12
  • 49
  • 84

2 Answers2

2

Using sed:

     sed 's/^/{"index":{}}\
/'

The trick here is the \.

Alternatively, if your shell supports it:

sed $'s/^/{"index":{}}\n/'

or (as per @sundeep's suggestion):

sed $'i\\\n{"index":{}}\n'

Using jq:

jq -nc 'inputs | {"index":{}}, .'

Here, the key is the -c option to produce JSONLines.

Using awk:

awk '{print "{\"index\":{}}"; print;}'

Etc.

peak
  • 105,803
  • 17
  • 152
  • 177
  • thanks but I didn't completely follow, say my input is this file: `data1.json`, my output is this name: `data2.json`, how will this command look like? thanks – Fisher Coder May 22 '21 at 14:38
  • Both sed and jq accept an input file as argument, so you could use the pattern: `COMMAND data1.json > data2.json` – peak May 22 '21 at 14:40
  • Could also use the `i` command instead of `s` command – Sundeep May 22 '21 at 14:43
  • perfect, this command worked: `jq -nc 'inputs | {"index":{}}, .' data2.json > data3.json` thanks a ton! – Fisher Coder May 22 '21 at 14:47
1

This might work for you (GNU sed):

sed 'i{"index":{}}' file

Insert {"index":{}} before each line.

potong
  • 55,640
  • 6
  • 51
  • 83