简体   繁体   English

根据文件中的时间戳对文件进行排序

[英]Sorting files based on a timestamp within the file

I have a bunch of files which contain an ascii header with a time stamp WITHIN the file, followed by a large chunck of binary data. 我有一堆包含一个ASCII头带有时间戳文件 ,跟着大chunck二进制数据的文件。 I would like to list the files sorted by this time stamp, at the command line (bash, etc). 我想在命令行(bash等)列出按此时间戳排序的文件。

The file headers look similar to the following: 文件头看起来类似于以下内容:

encoding: raw
endian: big
dimension: 4
sizes: 128 128 1 4
spacings: 1.0 1.0 1.0 NaN
position: -3164,-13678
date_time: 06.02.12.18:59
user_name: Operator1
sample_name: 
dwell_time: 4.000
count_time: 65.536
duration: 202.000
raster: 79912
pixel_width: 624.3125
pixel_height: 624.3125

....binary data....

I would like to sort based on the "date_time" time stamp, which uses the format dd.mm.yy.hh:mm 我想基于“ date_time”时间戳进行排序,该时间戳使用格式dd.mm.yy.hh:mm

The sort --key option looks promising but all my attempts have failed. sort --key选项看起来很有希望,但是我所有的尝试都失败了。 Any help is much appreciated. 任何帮助深表感谢。 Thanks. 谢谢。

Assuming these files are images, so you can use a tool like exiftool to rename them based on their creation dates, then sort them by name. 假设这些文件是图像,那么您可以使用exiftool类的工具根据其创建日期来重命名它们,然后按名称对其进行排序。

If you can't rename them, just dump the names with the creation date to STDOUT and sort that, eg: 如果您不能重命名它们,只需将带有创建日期的名称转储到STDOUT并对其进行sort ,例如:

exiftool -p '$dateTimeOriginal $filename' -q -f DIRECTORY/WHERE/IMAGES/ARE | sort -n 

If you only want the filenames in the output, append a | cut -f 2 -d " " 如果只需要输出中的文件名,请在文件名后附加| cut -f 2 -d " " | cut -f 2 -d " " to the end. | cut -f 2 -d " "到末尾。

If it's file format which is not recognized by exiftool this might or might not work: 如果是exiftool无法识别的文件格式,则可能会或可能不会起作用:

for f in YOURFILES* ; do
    filedate=`grep --binary-file=text -i -o 'date_time: ...........:..' $f | head -1`
    echo "$filedate $f"
done | sort -n

Note : this won't work when there are spaces in the filenames (and I'm leaving that to you to resolve). 注意 :当文件名中有空格时,这将不起作用(我将留给您解决)。 And if you want to output only the sorted filenames, append | awk '{print $NF}' 如果只想输出排序后的文件名,请添加| awk '{print $NF}' | awk '{print $NF}' after the sort -n . sort -n之后| awk '{print $NF}'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM