sanjuro8998 February 2016

Grep latest file string

System: AIX 7.1

Let's say I have a folder with files like these:

enter image description here

The files I need always have this prefix sapemea_postatus.log.xxxxxxxx, and inside of them we always have a number (collection number), like this:

enter image description here

And I always have questions like

Hey, which collection number is the last one we have?

So I always have to open the FTP, navigate to the folder, filter the column to show the newest ones in the top, look for the latest sapemea_postatus.log file, download it and check with notepad++.

So my question is: Is there a way to make grep command where I can filter for those kind of files, get the most recent one, and show this collection number? Thanks!

Answers


Harald Nordgren February 2016

#!/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)"

Post Status

Asked in February 2016
Viewed 3,786 times
Voted 10
Answered 1 times

Search




Leave an answer