Quantcast
Channel: Active questions tagged jq - Stack Overflow
Viewing all articles
Browse latest Browse all 524

How to properly format CSV output using jq and sed?

$
0
0

I'm trying to process a JSON file and convert it to a CSV format using jq and sed. My goal is to replace decimal points with commas in one of the fields and ensure the output is properly formatted with semicolons as delimiters. However, I'm encountering issues with escaping characters.

Here's the jq and sed script I'm using:

json_to_csv() {    input_file=$1 #json file    output_file=$2 #csv filejq -r '.[] |.MetaData as $meta |.Data[] |[    ($meta[1].Nombre),  #Group    ($meta[2].Nombre),  #Area    (.Valor | tostring | gsub("\\.";"\\,")) #Value] |@csv'"$input_file" | sed 's/"//g' |  # Remove quotes generated by @csvsed 's/\\,/,/g' |  # Remove the backslash escaping the commased 's/,/;/g'> "$output_file"  # Replace commas with semicolons# Add headersed -i '1iGroup;Area;Total'"$output_file"    return $?}

THe output I am getting is:

Group;Area;TotalA;2;11\;76A;2;11\;84

What i need is:

Group;Area;TotalA;2;11,76A;2;11,84

I have tried several variants, but I either get all semicolons, all commas, or a escaping bar


Viewing all articles
Browse latest Browse all 524

Trending Articles