-1

I'm making a program that will load data into table. I've already created the procedure. But I'm having a problem with handling the file, since there are multiple files in the directory with different timestamps.

I would like to ask what is the code to detect the LATEST timestamp of the feed and choose that one.

For example, the name of the feed is PRODUCTS_I03_20130429.dat where "PRODUCTS_I03" is just the name of the feed.

I just want to detect the latest feed through its timestamp.

Thank You in Advance!

4

3 回答 3

0
 ls *.dat | python -c "import sys,re; print sorted(sys.stdin, key = lambda x: re.search( r'(\d+)\.dat', x).group(1))[-1]"
于 2013-04-29T12:04:46.433 回答
0

如果您使用的是 shell 脚本或命令行,请尝试“ls -t”,或者如果您希望它们以相反的顺序使用,请使用“ls -tr”

于 2013-04-29T10:51:32.087 回答
0

你可以这样:

$ latest=$(echo -e "PRODUCTS_I03_20130429.dat\nPRODUCTS_I03_20130428.dat"|sort -r -t"_" -k3|head -1)
$ echo ${latest%_*}
PRODUCTS_I03

只有当文件名中有两个时它才会起作用_echo只是为了测试。您应该将其替换为您要检查的模式,例如ls -1 PRODUCTS_*_*.dat.

如果时间戳与文件的日期相同,您可以使用以下内容:

$ latest=$(ls -1rt|head -1)
$ echo ${latest%_*}

或者您可以使用获取(最后修改)时间戳的实际日期state -c %y $latest

于 2013-04-29T12:42:54.143 回答