0

I'm looking for a command or a simple procedure to get the date from the filename. My file name could have multiple date formats.

Eg:

Filename Format Output filename_2016-01-01 filename_%Y-%m-%d 2016-01-01 2016-01-01_filename %Y-%m-%d_filename 2016-01-01 20160101-filename %Y%m%d_filename 2016-01-01 1451606400-filename %s-filename 2016-01-01 

I'm looking for some procedure say if I specify my "Format" it will return date from that format as output. I'm not worried about format of output. I'm not expecting a huge script. Please let me know if any such functionality already exists.

If there is not other procedure, I'm planning to build one.

8
  • So it is not the time a file was created, rather a subset of the actual filename you want to return? Is it to be stored in a file? Output on screen? Give some pseudo-wanted output. Commented Jun 30, 2016 at 20:12
  • Yes. Date from file name. I'll update the output.. Commented Jun 30, 2016 at 20:12
  • Is the rest of the filename always known? For example, if it is always "Filename<datestuff>" or "<datestuff>_filename" or "<datestuff>-filename", can't you just strip away the known part? Commented Jun 30, 2016 at 20:14
  • 3
    These are just the requirements. Where is your code? Commented Jun 30, 2016 at 20:14
  • 1
    @Prashanth I'm not aware of an existing command which could achieve that. Commented Jun 30, 2016 at 20:21

1 Answer 1

3

Bash has variable dereferencing modification syntax that can handle some of what you want:

$ f=filename_2016-01-01 $ echo ${f#*_} 2016-01-01 $ f=2016-01-01_filename $ echo ${f%_*} 2016-01-01 $ f=20160101-filename $ echo ${f%-*} 20160101 $ f=1451606400-filename $ echo ${f%-*} 1451606400 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.