Valid and invalid date in the file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Valid and invalid date in the file
# 1  
Old 05-31-2012
Valid and invalid date in the file

Hi All,

How to validate the 4th column,it is date column in the file, if it valid move to valid file else moved invalid file.
Code:
9f680174-cb87|20077337254|0|20120511|N 
9f680174-cb88|20077337254|0|20120534|N

i want two file valid.txt and invalid.txt

Thanks,
# 2  
Old 05-31-2012
Code:
#!/usr/bin/perl

use Time::Local;
use strict;

open (VF,">valid-file")  || die "$! \n";
open (IF,">Invalid-file") || die "$! \n";

while (<DATA>) {
chomp;
my @flds=split(/\|/);
eval {
        timelocal(0,0,0,substr($flds[3],6,2),substr($flds[3],4,2),substr($flds[3],0,4));
     };
print IF "$_\n" if ($@);
print VF "$_\n" if !($@);
}

close(VF);
close(IF);

__DATA__
9f680174-cb87|20077337254|0|20120511|N
9f680174-cb88|20077337254|0|20120534|N


Last edited by pravin27; 05-31-2012 at 11:26 AM..
# 3  
Old 05-31-2012
Thanks... Do you any idea with AWK command...to validate the date column..
# 4  
Old 05-31-2012
most versions of awk don't have very good date-handling functions, so it'd mean rolling your own and worrying about leap years and other such mess. perl is probably the best way.
# 5  
Old 05-31-2012
Try...
Code:
awk -F '|' '{if(system("touch -t " $4 "0000 tempfile 2>/dev/null"))print > "invalid.txt"; else print > "valid.txt"}' file

# 6  
Old 05-31-2012
how about this ?
Code:
gawk  -F"|" '{
   if (match($4,/^((19|20)[0-9][0-9])(0[1-9]|1[012])(0[1-9]|[12][0-9]|3[0-1])$/,a)) {
      year=a[1]+0
      mon=a[3]+0
      day=a[4]+0
        if (day ==  31 && (mon == 4 ||  mon == 6 || mon == 9 || mon == 11))
        print $0 >  "valid.txt"
        else if (day >=  30 && mon == 2)
        print $0 >  "valid.txt"
        else if (mon == 2 && day ==  29 && ! (  year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)))
        print $0 > "valid.txt"
        else
        print $0 > "valid.txt"
   }  else { print $0 > "invalid.txt" }
   }' input_file

# 7  
Old 05-31-2012
With GNU date:

Code:
#!/bin/bash

while IFS='|' read a b c d e
do
    date -d "$d" > /dev/null 2>&1
    if [ $? -eq 0 ]
    then
        echo "$a|$b|$c|$d|$e" >> valid.txt
    else
        echo "$a|$b|$c|$d|$e" >> invalid.txt
    fi
done < inputfile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Valid separator in time and date format

Hello. I can use any particular (stupid or not) format when using bash date command. Example : ~> date --date "now" '+%Y-%m-%d %H!%M!%S' 2019-06-03 12!55!33or ~> date --date "now" '+%Y£%m£%d %H¤%M¤%S' 2019£06£03 12¤57¤36 or ~> date --date "now" '+%Y-%m-%d %H-%M-%S' 2019-06-03 12-58-51 ... (4 Replies)
Discussion started by: jcdole
4 Replies

2. Shell Programming and Scripting

Date: invalid date trying to set Linux date in specific format

i try to set linux date & time in specific format but it keep giving me error Example : date "+%d-%m-%C%y %H:%M:%S" -d "19-01-2017 00:05:01" or date +"%d-%m-%C%y %H:%M:%S" -d "19-01-2017 00:05:01" keep giving me this error : date: invalid date ‘19-01-2017 00:05:01' Please use CODE tags... (7 Replies)
Discussion started by: umen
7 Replies

3. UNIX for Advanced & Expert Users

Invalid date

Hi, I have a function to calculate "yesterday" in format YYYYMMDD: desa_ev9 # date +"%Y%m%d" --date "-1 day 20180701" 20180630 desa_ev9 # date +"%Y%m%d" --date "-1 day 20180720" 20180719 desa_ev9 # date +"%Y%m%d" --date "-1 day 20190101" 20181231 desa_ev9 # date +"%Y%m%d" --date "-1... (2 Replies)
Discussion started by: augreen
2 Replies

4. Shell Programming and Scripting

How to loop read command and print valid invalid states.?

My question is how would i loop a read command to keep asking the user for input and eventually print the no. of valid invalid inputs after a specified control input typed i.e. (-3). (1 Reply)
Discussion started by: Flowoftruth
1 Replies

5. UNIX for Dummies Questions & Answers

How to check if file contains valid strings?

Hi All, I am a newbie...I would like to have a function which ll check if a file contains valid strings before "=" operator. Just to give you my requirement: assume my file has content: hello= gsdgsd sfdsg sgdsg sgdgdg world= gggg hhhh iiiii xxxx= pppp ppppp pppp my... (5 Replies)
Discussion started by: rtagarra
5 Replies

6. Shell Programming and Scripting

date: invalid date

Hi All, I am trying to convert the date of all files under a directory in seconds, PFB script a=`ls -lrt | wc -l` echo $a for ((i=1;i<=$a;i++)) do A=`ls -lrt | awk '{print $6,$7,$8}' | head -$i | tail -1` echo ${A} date -d '${A}' +%s donebut I am getting error date: invalid date... (1 Reply)
Discussion started by: Jcpratap
1 Replies

7. Shell Programming and Scripting

Valid date checking in the file

File contian below data... 20111101 20111102 20111131 I am new to unix and per scirpt... First two records are valid and last record is invalid,how to get the valid records... Please help me unix or perl script. :wall: Thanks, Murali (5 Replies)
Discussion started by: muralikri
5 Replies

8. Shell Programming and Scripting

Need to get next valid date from date string

Hi, I am passing date string of format 'YYYYMMDD' to a ksh script. Will I be able to get next valid date from the passed in string. Example I pass '20100228' to the shell script, Is there a reverse date command to get '20100301' .i.e to convert '20100228' to date and get next date.... (5 Replies)
Discussion started by: bittoo
5 Replies

9. Shell Programming and Scripting

date: invalid date `19010101'

why date -d "19010101" gets error " date: invalid date `19010101' " but date -d "19020101" is fine? Any approach to fix that? Thank you. (8 Replies)
Discussion started by: freizl
8 Replies

10. Shell Programming and Scripting

Check valid records in really big file with one commend..

Hi, I have a 5 gig file, no record terminators, field terminators are newline. The record length is 768 and I would like to check that every 768th byte is a newline and print out the byte position if it isn't. I would like to do this going either forward or backwards with one command if... (3 Replies)
Discussion started by: vtischuk@yahoo.
3 Replies
Login or Register to Ask a Question