I have got few date format patterns like "yyyymmdd", "yy_mm_dd" etc.
There can be any combination of such patterns.
I have used add_delta_days to find "yyyy", "yy", "mm", "dd" for the current date and saved them to different variables like "$y1", "$y2", "$m1" etc
In one line, i want to check if the dateformat has yyyy/yy and replace it with the corresponding variable $y1.
Here the date format has yyyy, so i want to replace it with $y1 which has current year "2011"
i need to check and replace the string with my variable
How can this be done in simple one line?
Any help will be appreciated.Thanks in advance
Something like this?
#!/bin/bash
dateformat='yymmdd';
Y1=2011;
Y2=11;
m1=11; #---- current month november
d1=05; #---- current day
dateval=$(echo $dateformat | sed "s/yyyy/$Y1/;s/yy/$Y2/;s/mm/$m1/;s/dd/$d1/")
echo $dateval
--ahamed
Thanks ahamed.
Yes but i need to do this in perl.
Oops didn't see that... Try this...
#!/usr/bin/perl
my $Y1 = 2011;
my $Y2 = 11;
my $m1 = 11; #---- current month november
my $d1 = 05; #---- current day
my $dateformat = 'yyyymmdd';
$_=$dateformat;
s/yyyy/$Y1/g; s/yy/$Y2/g; s/mm/$m1/; s/dd/$d1/;
my $dateval=$_;
print $dateval;
--ahamed
This if fine but will make the script look big. I need to do this in a single line because there are 3 more patterns for month..."month", "mon" too.
The above four should be done in a single line.
I modified the code already... will that be fine?
--ahamed
Thanks ahamed. It works Perfect
Can you please explain me how it works?
You understood the substitution right...
s/yyyy/$Y1
Replace yyyy with the value of $Y1
$_ is a inbuilt variable. So if we do any operations without specifying the variables, it will be done on $_ automatically. So I assigned the value of $dateformat to $_ and executed the substitution operations. And then got it back!
HTH
--ahamed
1 Like
Oh. I got that. Thanks a bunch
---------- Post updated at 02:58 AM ---------- Previous update was at 02:23 AM ----------
I have one small clarification here.
In a particular case,
i may hav date format as "yyyymonthdd" which willl give "2011November11"
Also another format like "yyyymondd" which will give "2011Nov11"
In such cases i have used as below.
But since "mon" is present in "month",
It replaces as "NOVth" instead of "November"
Swap the commands...
s/yyyy/$y1/g; s/yy/$y3/g; s/mm/$m1/; s/month/$m4/; s/mon/$m2/; s/dd/$d1/;
--ahamed