Insert header for each document before uploading to elastic search

77 views Asked by At

I have a ndjson file with the below format

{"field1": "data1" , "field2": "data2"}
{"field1": "data1" , "field2": "data2"}
....

I want to add a header like

{"index": {}}

before each document before using the bulk operation I found a similar question: Elasticsearch Bulk JSON Data

The solution is this jq command:

jq -cr ".[]" input.json | while read line; do echo '{"index":{}}'; echo $line; done > bulk.json

But I get this error:

'while' is not recognized as a internal or external command

What am I doing wrong? Im running Windows Or is there a better solution? Thanks

1

There are 1 answers

0
Andreas Jägle On

The while in your sample is a construct that is usually built-in a developer-friendly shell like e.g. sh, bash or zsh but windows doesn't provide out of the box. See the bash docs for example.

So if this is a one-time thing, probably the fastest solution is to just use some text editor and add the required action lines by using some multi-cursor functionality.

On the other hand, if you are restricted to Windows but want some kind of better shell to use this more often, you should have a look at the cmder project that brings you a bash environment to your windows desktop when using the full version that is packaged with git-for-windows. This should allow you to use such scripting features even on a non linux or mac environment.