拆分 csv 文件:每天一个记录文件

发布于 2025-01-21 03:24:02 字数 570 浏览 3 评论 0原文

我有一个文件Metrics.csv包含来自不同传感器的几行记录。这是每行的标题和格式:

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
...

时间是在纳秒中给出的时间(我乘以前10位将其转换为日期)。 我想每天创建一个文件。 到目前为止,我做了这个循环,它使我可以访问每条线的日期:

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

感谢您的帮助,我是Bash的初学者。 非常感谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

温柔一刀 2025-01-28 03:24:02

这是我的解决方案:

#!/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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文