本文介绍了读取时间戳文件并创建目录结构取决于时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有年份数据的日志文件,我想在日志文件中读取并获取 unix 时间戳列并转换为人类可读的时间戳(不是字面上的,只是逻辑上的).取决于时间戳.我想创建一个基于时间的目录结构这样的东西(2017/05/20/13)(YYYY/MM/DD/HH)并将这些文件复制到各自的目录结构

I have log files for year's data , I want to read inside the log file and take the unix timestamp column and convert to human readable timestamp(not literally, only logically) . Depends on the timestamp .I want to create a Time based directory structure Something like this ( 2017/05/20/13 ) (YYYY/MM/DD/HH) and copy those files to respective directory structure

LANG=C
if [[ -z "$1" ]]
  then
if [[ -p /dev/stdin ]]    # input from a pipe
  then
    read -r p
 else
    echo "No timestamp given." >&2
    exit
 fi
  else
p=$1
fi
awk 'BEGIN {FS=OFS=":"}{$7=strftime("%Y-%m-%d %H:%M:%S",$7)} {print}' 
sample.txt

这是awk命令后的日志文件示例输出,现在我想创建一个文件夹取决于日期和小时.类似这样的

this is the Log file sample output after the awk command , Now I want to create a folder depends on the date and hour . Something like this

Input 
counter.txt
28:-58:31147:28:0:0:2017-05-24 15:12:50:3064:0:0:0:103
28:-58:31147:28:0:0:2017-05-24 15:12:50:3064:0:0:0:102
21:-60:17086:28:0:0:2017-05-24 15:12:50:1384:0:0:0:102
10:-64:16651:28:0:0:2017-05-24 16:12:50:656:0:0:0:103

文件夹结构类似[root@master 24-05-17]# ls -ltr |grep counter_-rw-rw-r-- 1 个用户 user 11974276 May 23 12:32 counter_25.txt-rw-rw-r-- 1 个用户 user 17271141 May 23 13:32 counter_24.txt-rw-rw-r-- 1 个用户 user 16819152 May 23 14:32 counter_23.txt

folder structure be like [root@master 24-05-17]# ls -ltr | grep counter_ -rw-rw-r-- 1 user user 11974276 May 23 12:32 counter_25.txt -rw-rw-r-- 1 user user 17271141 May 23 13:32 counter_24.txt -rw-rw-r-- 1 user user 16819152 May 23 14:32 counter_23.txt

输出就像:

/2017/05/24/15 - in this folder , we have to seperate the input file and write 
counter_2017_05_24_15.txt 
28:-58:31147:28:0:0:2017-05-24 15:12:50:3064:0:0:0:103
28:-58:31147:28:0:0:2017-05-24 15:12:50:3064:0:0:0:102
21:-60:17086:28:0:0:2017-05-24 15:12:50:1384:0:0:0:102

/2017/05/24/16 in this folder , I want to write the remaining data in another 
new file and store seperately under the folder 
counter_2017_05_24_16.txt
10:-64:16651:28:0:0:2017-05-24 16:12:50:656:0:0:0:103
23:-66:33444:28:0:0:2017-05-24 16:12:50:656:0:0:0:103
10:-64:16651:28:0:0:2017-05-24 16:12:50:656:0:0:0:102

推荐答案

代替这个:

strftime("%Y-%m-%d %H:%M:%S")

这样做:

strftime("%d/%m/%Y")

现在您有一个 DD/MM/YYYY 字符串,可以将其用作路径的一部分.

Now you have a DD/MM/YYYY string, and can use it as part of a path.

这篇关于读取时间戳文件并创建目录结构取决于时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 04:59