根据时间戳匹配最接近的文件名 [英] Match closest filename based on timestamp

查看:39
本文介绍了根据时间戳匹配最接近的文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有备份存储在 S3 中,需要根据最近的时间戳检索备份.我们有文件以 YYYYMMDD_HHMM.tar.gz 格式存储在 S3 中

We have backups stored in S3 and need to retrieve a backup based on nearest time stamp. We have files stored in S3 in YYYYMMDD_HHMM.tar.gz format

20181009_0910.tar.gz
20181004_0719.tar.gz
20180925_0414.tar.gz
20180915_2210.tar.gz

给定时间戳 20180922_1020,我们需要使用 shell 脚本获取文件 20180925_0414.tar.gz.

Given a timestamp 20180922_1020, we need to fetch file 20180925_0414.tar.gz using shell script.

谢谢.

推荐答案

使用 Perl One liner(当然有点冗长!!).

Using Perl One liner ( Of-course somewhat lengthy !!).

我知道最近"意味着任一方向的输入之间最短.也就是说,如果你有 2 个文件,Oct-1st.txt 和 Oct-30.txt,如果输入是 Oct-20,那么 Oct-30 文件将是输出

I understand that "nearest" means shortest between the input in either direction. That is if you have 2 files, Oct-1st.txt and Oct-30.txt, and if the input is Oct-20, then Oct-30 file will be the output

$ ls -l *2018*gz
-rw-r--r-- 1 xxxx xxxx 0 Oct 17 00:04 20180915_2210.tar.gz
-rw-r--r-- 1 xxxx xxxx 0 Oct 17 00:04 20180925_0414.tar.gz
-rw-r--r-- 1 xxxx xxxx 0 Oct 17 00:04 20181004_0719.tar.gz
-rw-r--r-- 1 xxxx xxxx 0 Oct 17 00:04 20181009_0910.tar.gz

$ export input=20180922_1020

$ perl -ne 'BEGIN { @VAR=@ARGV; $in=$ENV{input}; $in=~s/_//g;foreach(@VAR) {$x=$_;s/.tar.gz//g;s/_//g;s/(\d+)/abs($1-$in)/e;$KV{$_}=$x};$res=(sort keys %KV)[0]; print "$KV{$res}"} ' 2018*gz
20180925_0414.tar.gz

$ export input=20180905_0101

$ perl -ne 'BEGIN { @VAR=@ARGV; $in=$ENV{input}; $in=~s/_//g;foreach(@VAR) {$x=$_;s/.tar.gz//g;s/_//g;s/(\d+)/abs($1-$in)/e;$KV{$_}=$x};$res=(sort keys %KV)[0]; print "$KV{$res}"} ' 2018*gz
20180915_2210.tar.gz

$

希望,这会有所帮助!

这篇关于根据时间戳匹配最接近的文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆