문제

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.

도움이 되었습니까?

해결책

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/')
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top