How to extract date and time from filename?

Hi,

I'm totally new in sell script and working with a shell code. I want to extract the date and time from the filenames. The filenames are different but all of them begins with WI_ SCOPE_:

WI_SCOPE_DATA_CHANGE_2017-09-12_15-30-40.txt
WI_SCOPE_BACK_COMPLETE_QUEUE_2017-09-12_15-31-40.txt
WI_SCOPE_CURRENT_CHECK_QUEUE_2017-09-12_15-32-40.txt
WI_SCOPE_DAILY_PARTY_2017-09-12_15-33-40.txt

I want to write a for loop and read the files in current directory, then extract date and time, convert it to seconds and compare it with current time. If it is greater than 259200(30 days) , delete it. Then read the next file.

Can you please guide me how I can do this?

Thanks.

The find command with the -mtime and -maxdepth options can do that for you.

to start with:

echo 'WI_SCOPE_DATA_CHANGE_2017-09-12_15-30-40.txt' | sed 's/.*_\([^_][^_]*_[^.][^.]*\).*/\1/'

or

echo 'WI_SCOPE_DATA_CHANGE_2017-09-12_15-30-40.txt' | awk -F'[_.]' '{print $(NF-2),$(NF-1)}' OFS=_

Thank you for your reply.

There are more than 6 files with these filenames which begins with 'WI_SCOPE_'. Could you give me an example that how I can write a loop that read these files and extract the date?

Taking vgersh99's awk solution and modifying it slightly (you need Gnu AWK and Gnu date for this - my Ubuntu system has mawk installed as awk and Gnu AWK installed as gawk )

for fname in WI_SCOPE_*
do
   secs=$(date +%s --date=$(echo $fname | gawk -F'[_.]' '{print $(NF-2),gensub(/-/,":","g",$(NF-1))}' OFS=T))
   ### now you have file name in fname and file time AS SECONDS in secs
done

If your version of Linux has Gnu AWK as awk you can change the line to use awk

Andrew

Can you tell us what OS, version and shell you are using? The output from uname -a;ps would be fine.

How about using shell variable substitution?

Assuming that the input files are all in the current directory and start WI_SCOPE_ you could easily do:-

for file in WI_SCOPE_*
do
   filedate="${file##*_*_}"            # Chop off from the beginning the longest string that matches "*_*_", i.e. keep just from two underscore back
   filedate="${filedate%.txt}"         # Chop off the .txt part from the end
   echo "I have got ${filedate}"
done

If you have lots of files then the other methods would cost you in performance of starting processes all over the place for each file. This is all within the shell process.

This should work for both ksh and bash.

I hope that this helps and you can easily extend the loop as you need to. Does this get you started down the process? We can further split up the string to get the values and then do calculations on them if you need to. How far do you get?

Kind regards,
Robin

1 Like