Split a csv file : one file of records per day

Viewed 28

I have a file metrics.csv containing several lines of records from different sensors. Here is the header and the format of each line :

name,time,HMDT,LUMI,TEMP,id,location,type
metrics,1587529745754514305,,9,,24a89ddc-23c8-4d9f-9f5e-cff4eba32fb5,school,sensors
metrics,1587529745954605110,,,25.7,88cb0522-478a-456c-b63b-9c402b5e03b2,school,sensors
...

The time is given in nanoseconds (I take the first 10 bits in order to convert it to a date). I would like to create one file per day. Up to now, I did this loop, which allows me to access to the date of every line :

#!/bin/bash
while IFS=, read -r name time HMDT LUMI TEMP id location type
  do
    date -d @${time::10}
  done < metrics.csv

I would appreciate your help, I am a beginner in bash. Many thanks.

1 Answers

Here is my solution :

#!/bin/bash
while IFS="," read a b c
 do
  d=$(echo $b | cut -c -10)
  d=$(date -d "@$d" +"%F")
  echo "$a,$d,$c" >> $d.csv
done < metrics.csv
Related