Grep 最新文件字符串
Grep latest file string
系统:AIX 7.1
假设我有一个包含以下文件的文件夹:
我需要的文件总是有这个前缀 sapemea_postatus.log.xxxxxxxx,并且在它们里面我们总是有一个数字(collection 数字),就像这样:
而且我总是有这样的问题
Hey, which collection number is the last one we have?
所以我总是要打开 FTP,导航到文件夹,过滤列以在顶部显示最新的,查找最新的 sapemea_postatus.log文件,下载下来用notepad++查看。
所以我的问题是:有没有办法制作 grep 命令,我可以在其中过滤那些类型的文件,获取最新的文件,并显示这个 collection 数字?谢谢!
#!/bin/bash
latest_collection=0
regex="Collection:[ ]*([0-9]+)"
for log in sapemea_postatus.log.*; do
[[ $(cat $log) =~ $regex ]]
collection="${BASH_REMATCH[1]}"
if [[ $collection > $latest_collection ]]; then
latest_collection=$collection
latest_log=$log
fi
done
echo "Latest log: $latest_log (number $latest_collection)"
系统:AIX 7.1
假设我有一个包含以下文件的文件夹:
我需要的文件总是有这个前缀 sapemea_postatus.log.xxxxxxxx,并且在它们里面我们总是有一个数字(collection 数字),就像这样:
而且我总是有这样的问题
Hey, which collection number is the last one we have?
所以我总是要打开 FTP,导航到文件夹,过滤列以在顶部显示最新的,查找最新的 sapemea_postatus.log文件,下载下来用notepad++查看。
所以我的问题是:有没有办法制作 grep 命令,我可以在其中过滤那些类型的文件,获取最新的文件,并显示这个 collection 数字?谢谢!
#!/bin/bash
latest_collection=0
regex="Collection:[ ]*([0-9]+)"
for log in sapemea_postatus.log.*; do
[[ $(cat $log) =~ $regex ]]
collection="${BASH_REMATCH[1]}"
if [[ $collection > $latest_collection ]]; then
latest_collection=$collection
latest_log=$log
fi
done
echo "Latest log: $latest_log (number $latest_collection)"