【发布时间】: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 这样的目录。你能帮我吗??