-2

I have the file say sample1.txt in linux env with comma separated values: below

"zc52","FS_SE",20-JUN-19,"P1C217.DB.ATC.COM  - 636","" 
 "p214","FS_MG",16-OCT-20,"","" 
 "x901","FS_TC",01-APR-21,"","" 
 "r750","FS_SP",06-JUL-21,"",

I need the output in below format having 1 million records, how can i do it.

{"id":"zc52","role":"FS_SE","created_time_stamp":"20-JUN-19, q_id":"P1C217.DB.ATC.COM - 636"}
{"id":"p214","role":"FS_MG","created_time_stamp":"16-OCT-20"}
{"id":"x901","role":"FS_TC","created_time_stamp":"01-APR-21"}
{"id":"r750","role":"FS_SP","created_time_stamp":"06-JUL-21"}
prat_86
  • 91
  • 2
  • 10
  • 2
    Does this answer your question? [Converting CSV to JSON in bash](https://stackoverflow.com/questions/44780761/converting-csv-to-json-in-bash) – Vojin Purić Nov 30 '21 at 18:28
  • thanks, can you provide how can i add these into my script to get the above data . – prat_86 Nov 30 '21 at 18:32
  • 4
    Where are you stuck here? SO is here to help with existing code attempts. Please [edit] yours into the question. Thanks. – ggorlen Nov 30 '21 at 18:43

1 Answers1

1

I would choose an other language for this task because it's a little difficult to parse a quoted CSV accurately with bash.

With Ruby it's real easy:

echo 'id,role,created_time_stamp,q_id' |
ruby -rcsv -rjson -e 'CSV.new(ARGF, headers: true).each{|r| puts r.to_hash.reject{|_,v| v.empty?}.to_json}' - file.csv

file.csv:

"zc52","FS_SE",20-JUN-19,"P1C217.DB.ATC.COM  - 636",""
"p214","FS_MG",16-OCT-20,"",""
"x901","FS_TC",01-APR-21,"",""
"r750","FS_SP",06-JUL-21,"",""

output:

{"id":"zc52","role":"FS_SE","created_time_stamp":"20-JUN-19","q_id":"P1C217.DB.ATC.COM  - 636"}
{"id":"p214","role":"FS_MG","created_time_stamp":"16-OCT-20"}
{"id":"x901","role":"FS_TC","created_time_stamp":"01-APR-21"}
{"id":"r750","role":"FS_SP","created_time_stamp":"06-JUL-21"}

remark: What you want to generate isn't a valid JSON but a lump of JSON objects.

Fravadona
  • 13,917
  • 1
  • 23
  • 35