Date format check and replace string in PERL


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Date format check and replace string in PERL
# 1  
Old 11-05-2011
Date format check and replace string in PERL

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

Quote:
Sample:

$dateformat = 'yyyymmdd';
$Y1 = 2011;
$Y2 = 11;
$m1 = 11; ---- current month november
$d1 = 05; ---- current day

output needed is
20111105
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
# 2  
Old 11-05-2011
Something like this?
Code:
#!/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
# 3  
Old 11-05-2011
Thanks ahamed.
Yes but i need to do this in perl.
# 4  
Old 11-05-2011
Oops didn't see that... Try this...
Code:
#!/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

Last edited by ahamed101; 11-05-2011 at 04:06 AM..
# 5  
Old 11-05-2011
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.

Quote:
$dateformat =~ s/yyyy/$Y1/g;
$dateformat =~ s/yy/$Y2/g;
$dateformat =~ s/mm/$m1/;
$dateformat =~ s/dd/$d1/;
The above four should be done in a single line.
# 6  
Old 11-05-2011
I modified the code already... will that be fine?

--ahamed
# 7  
Old 11-05-2011
Thanks ahamed. It works Perfect Smilie
Can you please explain me how it works?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Check Date Format And Email Out

Hello All, I have a requirement where i need to get the EXTRACT_DATE from a file and check if the date is of valid format or not and then mail it if it is not valid. Appreciate if you can help me with this. I did the following so far. awk '{for(i=1;i++<=NF;)if($i~/^EXTRACT_DATE/) print $i}'... (11 Replies)
Discussion started by: Ariean
11 Replies

2. Shell Programming and Scripting

How to check if date format is correct?

Hi! how do i know if the input is the same as the required date format? the date should be dd/mm/YYYY ex. 2/3/2012 or 15/11/2012 all the following conditions must return an error: *input of string *day is > 31 or < 1 *month is > 12 or < 1 *year is < 2013 suppose the date format is stored... (1 Reply)
Discussion started by: angilulu
1 Replies

3. Shell Programming and Scripting

Perl code to check date and check files in particular dir

Hi Experts, I am checking how to get day in Perl. If it is “Monday” I need to process…below is the pseudo code. Can you please prove the code for below condition. if (today=="Monday" ) { while (current_time LESS THAN 9:01 AM) ... (1 Reply)
Discussion started by: ajaypatil_am
1 Replies

4. Shell Programming and Scripting

finding date numeral from file and check the validity of date format

hi there I have file names in different format as below triss_20111117_fxcb.csv triss_fxcb_20111117.csv xpnl_hypo_reu_miplvdone_11172011.csv xpnl_hypo_reu_miplvdone_11-17-2011.csv xpnl_hypo_reu_miplvdone_20111117.csv xpnl_hypo_reu_miplvdone_20111117xfb.csv... (10 Replies)
Discussion started by: manas_ranjan
10 Replies

5. Shell Programming and Scripting

PERL : hhmiss - Date format check and replace

I have a filename, This can be any of any format, I want to check if the filename has hours,mins and seconds part. If it is present, i want to replace it with a " * " (star symbol) output needed: IMP: The time part can be in any pattern. How can this be done?:confused:... (3 Replies)
Discussion started by: irudayaraj
3 Replies

6. Shell Programming and Scripting

Need to check date format yyyymm

My source file having one date column. The formate of the date column is yyyymm. I need to validate whether all the rows are in same format in the given file. If it is not I have captured that records in a separate file. I am very new to Unix. I don't how to achieve this. Plz help me to achieve... (2 Replies)
Discussion started by: suresh01_apk
2 Replies

7. Shell Programming and Scripting

PERL String to Date (Custom format yyyymmdd to dd-mon-yyyy)

Hi All, I am learning PERL for one of the projects, and in one of these scripts, I read a flat text file and print in the terminal. The problem is, the text file has a date field. The format is yyyymmdd. I need to display this as dd-mon-yyyy. Any ideas to do this? Thanks a lot for the... (9 Replies)
Discussion started by: guruparan18
9 Replies

8. Shell Programming and Scripting

Check input date format?

how to check input date format. for example $input_date must be in format dd.mm.gg script is execute like this: bin/script1.sh 14.12.2009 script1.sh code: #!/bin/sh input_date=$1 CMD="/app/si/test/test.sh $input_date" echo "*****" $CMD (2 Replies)
Discussion started by: waso
2 Replies

9. Shell Programming and Scripting

How to search a date format from a file an replace with a string in PERL

I am very new to Perl. I am struggling so hard to search a date (such as 10/09/2009, 10-09-2009) from a text file and replace with a string (say DATE) using Perl. Please help me out. Thanks in advance. Regds Doren (4 Replies)
Discussion started by: my_Perl
4 Replies

10. Shell Programming and Scripting

Check the format of Date

Hi, I have a date field in my input file. I just want to check if its in the format "DD-MM-YYYY". Is there any command which can achieve this? Thanks and Regards, Abhishek (2 Replies)
Discussion started by: AAA
2 Replies
Login or Register to Ask a Question