PERL : hhmiss - Date format check and replace


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting PERL : hhmiss - Date format check and replace
# 1  
Old 11-08-2011
PERL : hhmiss - Date format check and replace

I have a filename,

Quote:
my $file = "abc_yyyy_mm_dd_hhmiss.txt";
This can be any of any format,

Quote:
my $file = "abc_yyyy_mm_dd_hh:mi:ss.txt";
my $file = "abc_yyyy_mm_dd_hh/mi/ss.txt";
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:
Quote:
my $file = "abc_yyyy_mm_dd_*.txt";

IMP: The time part can be in any pattern.
How can this be done?Smilie
Any help willl be appreciated. Thanks in advance.

Last edited by irudayaraj; 11-08-2011 at 03:25 AM..
# 2  
Old 11-08-2011
PERL

Hi,

Try this one,

Code:
perl -e 'my $file="abc_yyyy_mm_dd_hh:mi:ss.txt";$file=~s/(.*).{8}\.txt$/\1*.txt/g;print $file;'

Cheers,
RangaSmilie
# 3  
Old 11-08-2011
Thanks Ranga. It works.

What does "8" signify here?
Also Can you please explain how this works?

---------- Post updated at 03:30 AM ---------- Previous update was at 03:19 AM ----------

I have a small clarification. This doesnt work for various patterns as shown below.

Code:
 
#!/usr/bin/perl
use strict;
use warnings;

my $filename = "abc_yyymmdd_hhmiss.txt";
$filename =~s/(.*).{8}\.txt$/\1*.txt/g;
print("file name is $filename");

Code:
file name is abc_yyymmd*.txt

can you please get me clarified on that

Last edited by pludi; 11-08-2011 at 05:06 AM..
# 4  
Old 11-08-2011
Code:
 
$ perl test.pl                                                                                                                                
file name is abc_yyymmdd_*.txt
file name is abc_yyyy_mm_dd_*.txt
file name is abc_yyyy_mm_dd_*.txt
file name is abc_yyyy_mm_dd_*.txt

 
$ cat test.pl
#!/usr/bin/perl
use strict;
use warnings;
my $filename = "abc_yyymmdd_hhmiss.txt";
$filename =~s/(.*_).*(.txt)/$1*$2/;
print("file name is $filename\n");

$filename = "abc_yyyy_mm_dd_hhmiss.txt";  
$filename =~s/(.*_).*(.txt)/$1*$2/;
print("file name is $filename\n");
$filename = "abc_yyyy_mm_dd_hh:mi:ss.txt";
$filename =~s/(.*_).*(.txt)/$1*$2/;
print("file name is $filename\n");
$filename = "abc_yyyy_mm_dd_hh/mi/ss.txt";  
$filename =~s/(.*_).*(.txt)/$1*$2/;
print("file name is $filename\n");

---------- Post updated at 03:44 PM ---------- Previous update was at 03:43 PM ----------

Code:
 
$filename =~s/(.*_).*(.txt)/$1*$2/;

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

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 In one line, i want to... (10 Replies)
Discussion started by: irudayaraj
10 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

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

8. 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

9. 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

10. Shell Programming and Scripting

Check for the correct date format in UNIx

Hi All, I am getting two input from User for Date from the command prompt when my script is executed . The date format i am taking is : DD-MM-YYYY so is there any method in Unix to validate the two input date. There might be many cases for these two date to be invalid.... (1 Reply)
Discussion started by: rawatds
1 Replies
Login or Register to Ask a Question