Issue
I have a requirement to convert a text file to below mentioned format.It consists with millions of lines of data.The format of text as below.
256456123
456852159
789123744
.........
This is the format i need after converting.The intention is to create below mongodb insert query.
db.database_name.insert([
{
"number":"256456123"
},
{ "number":"456852159"
},
{ "number":"789123744"
}
]);
How can I do this using bash script?
Solution
I assume you're looking for an array of objects, since your sample output isn't valid JSON as is.
Something like
sed -e '1s/.*/[{"number":"&"}/' \
-e '2,$s/.*/,{"number":"&"}/' \
-e '$s/$/]/' input.txt > output.json
will put brackets around it all, and by putting a comma before each object on all lines but the first, avoid issues with an extra trailing comma.
Answered By - Shawn