1

There are two date format used in shell script:

Format 1. 01-01-2014
Format 2. 2014-01-01

Scenario:
format 1 is a directory, while format 2 is file name.
I am trying to parse format 2 file using shell script, but stuck at one point.
I am managing call to directory having format 1 by defining all values in array, now for each array value I am parsing required file in format 2.
Now to manage array of format 2.
Problem can be solved if , I can do date conversion like in PHP.

$date='01-01-2014'
$filetosearch=date('Y-m-d',strtotime($date))

by doing above two steps I can get two different date format from one I define in array. so such thing happen in shell or not.

1
  • Is format 1 in American (month-day-year) or in European (day-month-year) style? Commented May 9, 2014 at 6:34

1 Answer 1

2

One solution, using awk:

date='31-01-2014'
filetosearch=$(echo "$date" | awk -F- '{print $3"-"$2"-"$1}')

And, using sed:

filetosearch=$(echo "$date" | sed 's/\(..\)-\(..\)-\(....\)/\3-\2-\1/')

The above assumes that date is in the European (day-month-year) format. If your date is in American format, then just a minor change is needed:

date='01-31-2014' 
filetosearch=$(echo "$date" | awk -F- '{print $3"-"$1"-"$2}')

And:

filetosearch=$(echo "$date" | sed 's/\(..\)-\(..\)-\(....\)/\3-\1-\2/')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.