【问题标题】:reading a timestamp file and creating a directory structure depends upon the timestamp读取时间戳文件并创建目录结构取决于时间戳
【发布时间】:2017-10-25 21:01:42
【问题描述】:

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

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 命令后的日志文件示例输出,现在我要创建一个取决于日期和小时的文件夹。像这样的

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 计数器_ -rw-rw-r-- 1 用户用户 11974276 5 月 23 日 12:32 counter_25.txt -rw-rw-r-- 1 用户用户 17271141 5 月 23 日 13:32 counter_24.txt -rw-rw-r-- 1个用户用户16819152 5月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

【问题讨论】:

  • 你有什么问题?
  • 嗨约翰,我有一个像这样的时间戳 1495532564。这是 unix 格式。上面的程序将转换为 IST 格式,但我的问题是我想创建一个目录取决于时间戳值。假设 17-05-2017 意味着我想创建一个像 17/05/2017 这样的目录。你能帮我吗??

标签: linux shell sh


【解决方案1】:

而不是这个:

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

这样做:

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

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

【讨论】:

  • 好吧当然。但是我怎样才能创建一个带有时间戳值的目录呢??
  • @user3427653:我建议mkdir
  • 我有一组值。比如 1/1/2015,1/1/2016,1/1/2017。我想检查时间戳并不断创建目录结构。示例我将运行 shell 脚本。它读取时间戳并根据值创建目录。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-01-20
  • 1970-01-01
  • 2018-07-09
  • 2014-06-22
  • 1970-01-01
  • 1970-01-01
  • 2013-06-01
相关资源
最近更新 更多