I have a CSV that looks like this:
created,id,value
2022-12-16 11:55,58,10
2022-12-16 11:55,59,2
2022-12-16 11:50,58,11
2022-12-16 11:50,59,3
2022-12-16 11:50,60,7
I want to parse it so I have the following result, setting ids as columns and grouping by date:
created,58,59,60
2022-12-16 11:55,10,2,nan
2022-12-16 11:50,11,3,7
missing values are set to nan, each id appears at most once per date
How can I do it? I also have the first CSV in a JSON equivalent if this is easier to do with jq
The JSON is composed of similar elements:
{
"created": "2022-12-16 09:15",
"value": "10.4",
"id": "60"
}
2
Answers
Using the great Miller (version >= 6), running
you get
The core command here is
reshape -s id,value
, to transform your input from long to wide structure.This is how I would do it in jq, based on the JSON input stream:
Then, either use the
@csv
builtin which wraps the values in quotes, and produces empty values for missing combinations:Demo
Or generate
nan
and,
manually bymap
ping andjoin
ing accordingly:Demo