How to extract date and time from filename?


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers How to extract date and time from filename?
# 1  
Old 10-19-2017
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_:

Code:
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.
# 2  
Old 10-19-2017
The find command with the -mtime and -maxdepth options can do that for you.
# 3  
Old 10-19-2017
to start with:
Code:
echo 'WI_SCOPE_DATA_CHANGE_2017-09-12_15-30-40.txt' | sed 's/.*_\([^_][^_]*_[^.][^.]*\).*/\1/'

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


Last edited by vgersh99; 10-19-2017 at 12:11 PM..
# 4  
Old 10-20-2017
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?
# 5  
Old 10-20-2017
Quote:
Originally Posted by Home
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)

Code:
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
# 6  
Old 10-20-2017
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:-
Code:
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

Last edited by rbatte1; 10-20-2017 at 06:07 AM.. Reason: Questions about OS & shell
This User Gave Thanks to rbatte1 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Extract date and time part from filename

Hi, I am facing one scenario in which I need to extract exact position of date and time from the name of the files. For example, Below is the record in which I need to extract position of YYYYMMDD,HHMISS and YYMMDD. Date and time variables can come more than once. I need to use these position... (13 Replies)
Discussion started by: Prathmesh
13 Replies

2. Shell Programming and Scripting

Validate date and time in filename by awk

hi i want to validate the date and time in filename filename : mohan.moh.ccyymmdd.ccyymmdd.hhmmss.txt mohan_moh.20151222.20151222.122442.txt i want code that check that date given in filename 20151222 in this format ccyymmdd else it mark file is not valid used in my OS detail is AIX 6... (12 Replies)
Discussion started by: MOHANP12
12 Replies

3. UNIX for Dummies Questions & Answers

Extract Date part from the filename

Hi All, I have incoming source files abcmmyy.txt I need to extract the mmyy part from the filename and pass that to a variable . I really appreciate your quick response on this. Thanks raj (7 Replies)
Discussion started by: rajeevm
7 Replies

4. Shell Programming and Scripting

append a filename with system date and time

Hi, There are similar kind of posts, but none seems like working for me. Please correct me if I'm wrong. I need append/rename file abc.txt with file processed date and time like abc_systemdatetimestamp.txt and move it to different folder. for example I have /source/data/abc.txt ... (1 Reply)
Discussion started by: amsn08
1 Replies

5. Shell Programming and Scripting

Extract date from filename and set timestamp

I have lots of files in this format: dvgrab-2003.06.29_15-30-24.mpg The numbers represents the date and time (YYYY.MM.DD_HH-MM-SS) How can I extract the dates from the filenames, and use the dates in the file timestamp? I guess this can be done by using "find", "sed" and "touch"? Can... (6 Replies)
Discussion started by: qwerty1234
6 Replies

6. Shell Programming and Scripting

Extract date from filename and create a new file

Hi, i have a filename CRED20102009.txt in a server 20102009 is the date of the file ddmmaaaa format the complete route is /dprod/informatica/Fuentes/CRED20102009.csv i want to extract the date to create a new file named Parameters.txt I need to create Parameters.txt with this... (6 Replies)
Discussion started by: angel1001
6 Replies

7. UNIX for Dummies Questions & Answers

Adding Date & time stamps to filename

I need to edit the file name with date and time while writing the script. please help. (1 Reply)
Discussion started by: manish.s
1 Replies

8. UNIX for Advanced & Expert Users

find formatted filename with date time

Hi, I operate and use HF radars along the California coast for ocean surface currents. The devices use Mac OS as the control and logging software. The software generates thousands of files a week and while I've used PERL in the past to solve the problems of finding files I come to realize some... (6 Replies)
Discussion started by: dpath2o
6 Replies

9. UNIX for Dummies Questions & Answers

Insert date/time within a filename

Hi Guys, I need to script the renaming of files as followins: files: firstjd secondjo thirdjv My script needs to insert the date/time infront of the last 2 characters of the filenames above, any ideas greatly received :) the letters before the last 2 characters could change, I'm only... (7 Replies)
Discussion started by: cooperman
7 Replies

10. UNIX for Dummies Questions & Answers

Renaming files to have date/time in filename

I have a program that will export my data to a single file, but it assigns a file name that is overridden every time I run the program. I need to change the file name to have a sequential number in the filename. How do I rename a file so that the filename contains the system date and time. I want... (5 Replies)
Discussion started by: wayneb
5 Replies
Login or Register to Ask a Question