shell 将时间戳修改为时间

需求说明

csv 文件以逗号拆分列,时间戳为 毫秒级别的

> 下载日志文件,日志内有时间戳,展示不够直观。

预期:将毫秒的时间戳修改为时间。

完整代码

replace.sh

cat replace.sh

#!/bin/bash
fileName=$1
outputFileName=$2
echo '' >  $outputFileName;
cat $fileName | while read line
do
  getDate=$(echo "$line" | awk -F ','  '{print  $2}')
  dateTime=$(date -j -f "%s" "${getDate: 0: 10}" "+%Y-%m-%d %H:%M:%S");
  replace=$(echo "$line" | sed "s/$getDate/$dateTime.${getDate: 10: 13}/g");
  
  echo $replace >>  $outputFileName;
done

执行替换操作

sh ./replace.sh inputFile.csv output.csv

代码解析

逐行读取文件

#!/bin/bash

cat $fileName | while read line
do
  echo $linedone

解析出时间戳在的列

csv 以逗号拆分列,且时间戳 第二列中。

#!/bin/bash

cat $fileName | while read line
do
  getDate=$(echo "$line" | awk -F ','  '{print  $2}')
done

拆分时间戳并转化为时间

#!/bin/bash
cat $fileName | while read line
do
  getDate=$(echo "$line" | awk -F ','  '{print  $2}')
  ## 拆分时间戳并转化为时间
  dateTime=$(date -j -f "%s" "${getDate: 0: 10}" "+%Y-%m-%d %H:%M:%S");
done

替换当前行中的时间戳

#!/bin/bash
cat $fileName | while read line
do
  getDate=$(echo "$line" | awk -F ','  '{print  $2}')
  dateTime=$(date -j -f "%s" "${getDate: 0: 10}" "+%Y-%m-%d %H:%M:%S");
  ## 替换当前行中的时间戳
  replace=$(echo "$line" | sed "s/$getDate/$dateTime.${getDate: 10: 13}/g");
done

将替换后的行写入到新的文件

#!/bin/bash
cat $fileName | while read line
do
  getDate=$(echo "$line" | awk -F ','  '{print  $2}')
  dateTime=$(date -j -f "%s" "${getDate: 0: 10}" "+%Y-%m-%d %H:%M:%S");
  ## 替换当前行中的时间戳
  replace=$(echo "$line" | sed "s/$getDate/$dateTime.${getDate: 10: 13}/g");
  ## 将替换后的行写入到新的文件
  echo $replace >>  $outputFileName;
done

excel 中单元格格式调整

yyyy/m/d hh:mm:ss.000

在这里插入图片描述

结果展示

在这里插入图片描述

Excel

=(B2/1000+8*3600)/86400+70*365+19
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

从未、淡定

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值