Visit The New, Modern Unix Linux Community

convert date inside a file

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting convert date inside a file
# 1  
convert date inside a file

Hi guys

I've got a file with this line inside.


The line always begins with number 100 or number 200, follow the year, the day in the year, the hour, and other stuff

Here, the important fields are, 2, 3 and 4.
-Filed 2 --> year
-Field 3 --> the day of the year (date +%j)
-Filed 4 --> time, can be from 3 or 4 characters. From 0 to 9 AM it has 3 characters, from 10 to 23 it has 4 characters. Example: 910 - 9:10AM, 21:30 - 9:30PM
What I need?

To get the date and time and compare with the real time of the server, and if it differ from 5 min, it sends a mail to root.

I've been 3 days trying to do it without success. Can help me?

# 2  
Do u have perl installed in ur machine?
# 3  
Rohon.. not sure now.. but I installe it if I it's needed.. so, go ahead! :-)
# 4  
See, perl has inbuilt functions for date and time comparison. So, it would be better to use.

I have written a code for date and time comparison for you. This may help you.


    # Packages to import functions
use Date::DateCalc qw(:all);
use Date::Calc;
use FileHandle;

    # Name of your i/p file
my $inpFile = "yourInp.txt";

    # Open file for reading
open FF, "<$inpFile";
while (<FF>) {
    chomp $_;
    my $timeDiff;

    # Get the server current date and time
    my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) = localtime(time);
    $year += 1900;
    $mon  += 1;

    # Get the current line's date and time
    my ( $cYear, $cYday, $cTime ) = split(/\,/,$_);
    my $hr_s;
    my $min_s;
    if ( $cTime < 1000 ) {
    } else {

    my $startTimeMin=$hr_s*60 + $min_s;
    my $endTimeMin=$hour*60 + $min;

    # Calculate date diff (server date - current line date)
    # and calculate timediff only if it is greater then 0
    my $ydayDiff = $yday - $cYday;
    $timeDiff=$endTimeMin - $startTimeMin if ($ydayDiff > 0);

    # Mail if timediff is greater then 5
    my $cmd = "mailx -s \"subject\"";
    system("$cmd") if ($timeDiff > 5);
close FF;

Revert if any doubtSmilieSmilie
# 5  
Really appreciate Rohon.. I'll test the code ASAP.. will let you know... thanks A LOT!


---------- Post updated at 04:26 PM ---------- Previous update was at 06:10 AM ----------

Hi Rohon.. it worked!! :-) Thanks

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #735
Difficulty: Medium
The Commodore 64 was shipped with BASIC v2.0 in RAM.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find week of the year for given date using date command inside awk

Hi all, Need an urgent help on the below scenario. script: awk -F"," 'BEGIN { #some variable assignment} { #some calculation and put values in array} END { year=#getting it from array and assume this will be 2014 month=#getting it from array and this will be 05 date=#... (7 Replies)
Discussion started by: vijaidhas
7 Replies

2. Shell Programming and Scripting

Searching a file inside a .tar.gz file by date

Hi, I would like to ask if there is a way to search for a file inside a .tar.gz file without extracting it? If there is, is there a way to search for that file by date? Thanks! (4 Replies)
Discussion started by: erin00
4 Replies

3. Shell Programming and Scripting

How to find the Missing date inside the FILE?

Hi am using Unix AIX Ksh have a FILE CAT FILE 08/02/2013 16/02/2013 18/02/2013 I need the Outputs as Missing date are 09/02/2013 to 15/02/2013,17/02/2013 can anyone help me !!! (1 Reply)
Discussion started by: Venkatesh1
1 Replies

4. Shell Programming and Scripting

Searching for unknown date inside the file and replace to new date

Hello, Iam a newbies to Shell scripting. Iam trying to replace the date inside the file to new date. is there anyway that we can just use the pattern to search as "..." I have many files want to replace with the same date, and each file contains different date. Thanks for your help. ... (2 Replies)
Discussion started by: Daro
2 Replies

5. Shell Programming and Scripting

Convert Date from File and Calculate Duration

Hi - I am looking for a little help to read in 2 date fields from a file in format: 20120508134012.3 yyyymmddhhmmss.tenths of a second So i want to: 1. Read in the 1st date from the file 2. Read in the second date from the file 3. Calculate the difference in minutes (or seconds) 4. ... (5 Replies)
Discussion started by: Newbie2012
5 Replies

6. Shell Programming and Scripting

ksh compare dates INSIDE a file (ie date A is > date B)

In KSH, I am pasting 2 almost identical files together and each one has a date and time on each line. I need to determine if the first instance of the date/time is greater than the 2nd instance of the date/time. If the first instance is greater, I just need to echo that line. I thought I would... (4 Replies)
Discussion started by: right_coaster
4 Replies

7. Shell Programming and Scripting

Need a unix script to convert date into Julian format in a text file

The 6th & 7th column of the text files represents date & time. I need this to be converted in julian format using command "date +%s -d <date>". I know the command, but dont know how to use it on the script 0 dbclstr-b IXT_Web Memphis_Prod_SQL_Full Memphis-Prod-SQL-Full-Application-Backup... (4 Replies)
Discussion started by: ajiwww
4 Replies

8. Shell Programming and Scripting

Convert the date format from mdy to ymd in column of file

The date format in the delimited file for one column '6/27/2011 12:00:00 AM' Is it possible o change it to '2011-06-27 12:00:00 AM' for all the records.. Thanks in advance..... (8 Replies)
Discussion started by: infernalhell
8 Replies

9. Shell Programming and Scripting

Edit date entry inside a file

Hi All, I wanted to edit the date value located at /var/opt/CPsuite-R65/fw1/conf/local.scv. The date entry looks like this : :Signature (">=20100717") How can I update the date value by 1 day every other day while preserving the margins of the whole file in a shell script? I have... (5 Replies)
Discussion started by: achillesxv
5 Replies

10. Shell Programming and Scripting

convert date format to mysql date format in log file

I have a comma delimited log file which has the date as MM/DD/YY in the 2nd column, and HH:MM:SS in the 3rd column. I need to change the date format to YYYY-MM-DD and merge it with the the time HH:MM:SS. How will I got about this? Sample input 02/27/09,23:52:31 02/27/09,23:52:52... (3 Replies)
Discussion started by: hazno
3 Replies

Featured Tech Videos