Extract date from filename and set timestamp


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract date from filename and set timestamp
# 1  
Old 07-18-2011
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 anyone help, please?

BR,
Q
# 2  
Old 07-18-2011
using find to list and sed to switch the format to a timestamp touch can use we get the following work of art.
Code:
for i in $(find $document_root -name dvgrab-\*.mpg) ; do
   touch -t $(echo $i | sed s/dvgrab-\([0-9]\{4\}\)\.([0-9]\{2\}\)\.\([0-9]\{2\}\)_\([0-9]\{2\}\)-\([0-9]\{2\}\)-\([0-9]\{2\}\)/\1\2\3\4\5\6/')  $i
done

# 3  
Old 07-18-2011
Quote:
Originally Posted by Skrynesaver
using find to list and sed to switch the format to a timestamp touch can use we get the following work of art.
Code:
for i in $(find $document_root -name dvgrab-\*.mpg) ; do
   touch -t $(echo $i | sed s/dvgrab-\([0-9]\{4\}\)\.([0-9]\{2\}\)\.\([0-9]\{2\}\)_\([0-9]\{2\}\)-\([0-9]\{2\}\)-\([0-9]\{2\}\)/\1\2\3\4\5\6/')  $i
done

Thanks for your suggestion, but this didn't do much. Could there be a typo somewhere?

BR,
Q
# 4  
Old 07-18-2011
Code:
echo 'dvgrab-2003.06.29_15-30-24.mpg' |\
while read line
do
date_=`echo ${line#*-}|sed 's/_/ /;s/.mpg$//;s/-/:/g;s/\./-/g'`
echo "date: $date_"
echo "timestamp: "`date -d "$date_" "+%s"`
done
date: 2003-06-29 15:30:24
timestamp: 1056925824

# 5  
Old 07-19-2011
Quote:
Originally Posted by Skrynesaver
using find to list and sed to switch the format to a timestamp touch can use we get the following work of art.
Code:
for i in $(find $document_root -name dvgrab-\*.mpg) ; do
   touch -t $(echo $i | sed s/dvgrab-\([0-9]\{4\}\)\.([0-9]\{2\}\)\.\([0-9]\{2\}\)_\([0-9]\{2\}\)-\([0-9]\{2\}\)-\([0-9]\{2\}\)/\1\2\3\4\5\6/')  $i
done

I've done an attempt to fix your "work of art" Smilie
Code:
for i in $(find $document_root -name dvgrab-\*.mpg) ; do
    touch -t $(echo $i | sed 's/dvgrab-\([0-9]\{4\}\)\.\([0-9]\{2\}\)\.\([0-9]\{2\}\)_\([0-9]\{2\}\)-\([0-9]\{2\}\)-\([0-9]\{2\}\).mpg/\1\2\3\4\5.\6/')  $i;
done

My problem now is that touch tries to set the timestamp with the directories in front. How can I strip out the leading dirs?

Code:
touch: invalid date format `./dir2/200306291530.24'
touch: invalid date format `./dir1/200306291004.25'

Any help appreciated!

BR,
Q
# 6  
Old 07-19-2011
Try with
Code:
basename "$i"
#instead of
echo $i

# 7  
Old 07-19-2011
MySQL

This did the trick:

Code:
for i in $(find $document_root -name dvgrab-\*.mpg) ; do
     touch -t $(echo $i | sed 's/.*dvgrab-\([0-9]\{4\}\)\.\([0-9]\{2\}\)\.\([0-9]\{2\}\)_\([0-9]\{2\}\)-\([0-9]\{2\}\)-\([0-9]\{2\}\).mpg/\1\2\3\4\5.\6/')  $i;
 done

Thanks a lot for your help!
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. UNIX for Beginners Questions & Answers

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... (5 Replies)
Discussion started by: Home
5 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

Filename timestamp

Hi Gurus, I have different files with different timestamp and different base file name, I have to group those files based on basename and provide a unique file name for similar file names. My Directory has following files. abc_filename_20130623:00:09:00.txt... (1 Reply)
Discussion started by: user_linux
1 Replies

5. Programming

to extract all the part of the filename before a particular word in the filename

Hi All, Thanks in Advance I am working on a shell script. I need some assistance. My code: if then set "subscriber" "promplan" "mapping" "dedicatedaccount" "faflistSub" "faflistAcc" "accumulator"\ "pam_account"; for i in 1 2 3 4 5 6 7 8;... (0 Replies)
Discussion started by: aealexanderraj
0 Replies

6. UNIX for Dummies Questions & Answers

to extract all the part of the filename before a particular word in the filename

Hi All, Thanks in Advance I am working on a shell script. I need some assistance. My Requirement: 1) There are some set of files in a directory like given below OTP_UFSC_20120530000000_acc.csv OTP_UFSC_20120530000000_faf.csv OTP_UFSC_20120530000000_prom.csv... (0 Replies)
Discussion started by: aealexanderraj
0 Replies

7. Shell Programming and Scripting

Check if a date field has date or timestamp or date&timestamp

Hi, In a field, I should receive the date with time stamp in a particular field. But sometimes the vendor sends just the date or the timestamp or correctl the date&timestamp. I have to figure out the the data is a date or time stamp or date&timestamp. If it is date then append "<space>00:00:00"... (1 Reply)
Discussion started by: machomaddy
1 Replies

8. 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

9. Shell Programming and Scripting

How to extract timestamp from the filename?

Hi., My file name is of the format: name_abc_20100531_142528.txt where., my timestamp is of the format: yyyymmdd_hhmmss How to extract the date strring and time string into seperate variables in the shell script, after reading the file as the input? I want to get the variables... (9 Replies)
Discussion started by: av_vinay
9 Replies

10. Shell Programming and Scripting

Timestamp in the filename

Hi i want to replace the previous time stamp with the current timsatp at the start of the file like 20090710_113354_FT0710a.txt this one to 20091111__113354_FT0710a.txt thanks in advance (3 Replies)
Discussion started by: Reddy482
3 Replies
Login or Register to Ask a Question