Compare a date (mm/dd/yyyy) with awk


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Compare a date (mm/dd/yyyy) with awk
# 1  
Old 05-30-2019
Compare a date (mm/dd/yyyy) with awk

Moderator's Comments:
Mod Comment Please use code tags to format data sets See the </> selection above your edit box

I have a bash script to extract data based on information in one column and I would like to add a date comparison in another column.


below is my file

cat foo
Code:
Info1.a   ,Info1.b   ,yes   ,Info1.d   ,Info1.e   ,Info1.f   ,01/01/2011   ,Info1.g   Info1.h   ,Info1.i   ,Info1.j   ,Info1.k   ,Info1.l
Info2.a   ,Info2.b   ,no    ,Info2.d   ,Info2.e   ,Info2.f   ,02/01/2012   ,Info2.g   Info2.h   ,Info2.i   ,Info2.j   ,Info2.k   ,Info2.l
Info3a   ,Info3.b   ,no    ,Info3.d   ,Info3.e   ,Info3.f   ,03/01/2013   ,Info3.g   Info3.h   ,Info3.i   ,Info3.j   ,Info3.k   ,Info3.l
Info4.a   ,Info4.b   ,yes   ,Info4.d   ,Info4.e   ,Info4.f   ,04/01/2014   ,Info4.g   Info4.h   ,Info4.i   ,Info4.j   ,Info4.k   ,Info4.l
Info5.a   ,Info5.b   ,yes   ,Info5.d   ,Info5.e   ,Info5.f   ,05/01/2015   ,Info5.g   Info5.h   ,Info5.i   ,Info5.j   ,Info5.k   ,Info5.l
Info6.a   ,Info6.b   ,no    ,Info6.d   ,Info6.e   ,Info6.f   ,06/01/2016   ,Info6.g   Info6.h   ,Info6.i   ,Info6.j   ,Info6.k   ,Info6.l
Info7.a   ,Info7.b   ,no    ,Info7.d   ,Info7.e   ,Info7.f   ,07/01/2017   ,Info7.g   Info7.h   ,Info7.i   ,Info7.j   ,Info7.k   ,Info7.l
Info8.a   ,Info8.b   ,yes   ,Info8.d   ,Info8.e   ,Info8.f   ,08/01/2018   ,Info8.g   Info8.h   ,Info8.i   ,Info8.j   ,Info8.k   ,Info8.l
Info8.a   ,Info8.b   ,no    ,Info8.d   ,Info8.e   ,Info8.f   ,09/01/2019   ,Info8.g   Info8.h   ,Info8.i   ,Info8.j   ,Info8.k   ,Info8.l

When I run the following command it returns nothing:
awk -F"," -v somedate="01/02/2015" '($3 == "yes" && $7 > somedate)' foo

However when I run the following command it returns some data but not what I am looking for:
sed "s/ //g" foo | awk -F"," -v somedate="01/02/2015" '($3 == "yes" && $7 > somedate)'
Code:
Info4.a,Info4.b,yes,Info4.d,Info4.e,Info4.f,04/01/2014,Info4.gInfo4.h,Info4.i,Info4.j,Info4.k,Info4.l
Info5.a,Info5.b,yes,Info5.d,Info5.e,Info5.f,05/01/2015,Info5.gInfo5.h,Info5.i,Info5.j,Info5.k,Info5.l
Info8.a,Info8.b,yes,Info8.d,Info8.e,Info8.f,08/01/2018,Info8.gInfo8.h,Info8.i,Info8.j,Info8.k,Info8.l

I would like to not remove the spaces and I would also like to have it return ony the following lines:
Code:
Info5.a   ,Info5.b   ,yes   ,Info5.d   ,Info5.e   ,Info5.f   ,05/01/2015   ,Info5.g   Info5.h   ,Info5.i   ,Info5.j   ,Info5.k   ,Info5.l
IInfo8.a   ,Info8.b   ,yes   ,Info8.d   ,Info8.e   ,Info8.f   ,08/01/2018   ,Info8.g   Info8.h   ,Info8.i   ,Info8.j   ,Info8.k   ,Info8.l

Any suggestions would be greatly appreciated.

Last edited by jim mcnamara; 05-30-2019 at 12:45 PM..
# 2  
Old 05-30-2019
convert MM/DD/YYYY to YYYYMMDD and compare numerically.
# 3  
Old 05-30-2019
Quote:
Originally Posted by vgersh99
convert MM/DD/YYYY to YYYYMMDD and compare numerically.
I thought about that but I do not have control on the date format in the file and its several thousand lines long. Do you have any suggestions on how to convert the date? Some of the other columns would have dates in them as well. But I am only worried about comparing the data in the 7th column right now and I will process the other dates later once I have narrowed down the file with this command.
# 4  
Old 05-30-2019
how about for starters...
awk -F, -v sd='01/02/2015' -f old.awk myFile where old.awk is:
Code:
function conv(d,  t) {
   split(d, t, "/")
   return (sprintf("%d%.02d%.02d", t[3], t[1], t[2]))
}
BEGIN {
  tab=sprintf("\t")
  somed=conv(trim(sd))
}

function trim(str)
{
    sub("^([ ]*|" tab "*)", "", str);
    sub("([ ]*|" tab "*)" "$", "", str);
    return str;
}

trim($3) == "yes" && conv(trim($7))>=somed

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Date format YYYY/MM/DD to DD/MM/YYYY

I am getting output of YYYY-MM-DD and want to change this to DD/MM/YYYY. When am running the query in 'Todd' to_date(column_name,'DD/MM/YYYY') am getting the required o/p of DD/MM/YYYY, But when am executing the same query(Netezza) in linux server(bash) am getting the output of YYYY-MM-DD file... (3 Replies)
Discussion started by: Roozo
3 Replies

2. UNIX for Dummies Questions & Answers

Epoch date to YYYY/MM/DD or MM/DD/YYYY

I've seen a lot of posts on this and have tried the following: echo 1257000000| perl -e '($d,$m,$y)=(localtime(time-86400));$m+=1;$y+=1900;printf "$y/$m/$d\n";' But I am unable to convert a past Epoch date into a format such as YYYY/MM/DD or MM/DD/YYYY. I am using bash and don't know... (4 Replies)
Discussion started by: newbie2010
4 Replies

3. Shell Programming and Scripting

Date conversion help from dd/mm/yyyy to dd/Mon/yyyy i.e. 28/10/2012 to 28/Oct/2012

Hi I have a problem with Date format in my code. 1st I am trying to convert today's date to yesterday's using YESTERDAY3=`perl -e '@y=localtime(time()-86400); printf "%04d/%02d/%02d",$y+1900,$y+1,$y;$y;'` And once it is done I am trying to using the yesterday date in a grep command to... (3 Replies)
Discussion started by: nithinankam
3 Replies

4. Shell Programming and Scripting

Need date in the format [mm dd yyyy hh AM/PM]

could you please help be on the below code .. Requirement is when i pass the parameter(for below 2) i should get current time -2 hours in the format :wall:.. cur_dt=`$ICEBIN/sqsh -S$DSQUERY -U $BATCHID -P $PASSWD -h -C"select getdate()" | sed '2d'` pr_dt="`$ICEBIN/sqsh -S$DSQUERY -U $BATCHID... (2 Replies)
Discussion started by: karthicss
2 Replies

5. Shell Programming and Scripting

awk compare column with date format

I have this code to compare columns 1 and 10 between file1 and file 2 and give me all records that match column 1 but dont match column 10 However column 10 is date format mm/dd/yy and awk cant read it and compare ...i tried awk < file1 -F~ '{print $10}' and it gave blank screen Is... (1 Reply)
Discussion started by: sigh2010
1 Replies

6. Shell Programming and Scripting

Converting date DD MM YYYY to DD MON YYYY

Hello, I am writing a script that parses different logs and produces one. In the source files, the date is in DD MM YYYY HH24:MI:SS format. In the output, it should be in DD MON YYY HH24:MI:SS (ie 25 Jan 2010 16:10:10) To extract the dates, I am using shell substrings, i.e.: read line ... (4 Replies)
Discussion started by: Adamm
4 Replies

7. Shell Programming and Scripting

change date format from yyyy/mm/dd to dd/mm/yyyy

(Attention: Green PHP newbie !) I have an online inquiry form, delivering a date in the form yyyy/mm/dd to my feedback form. If the content passes several checks, the form sends an e-mail to me. All works fine. I just would like to receive the date in the form dd/mm/yyyy. I tried with some code,... (6 Replies)
Discussion started by: keyboarder
6 Replies

8. Shell Programming and Scripting

converting the date field from dd/mm/yyyy to yyyy/mm/dd

How to convert the date field from dd/mm/yyyy to yyyy/mm/dd in unix my script will generate text file which have two fields one is date and another is name of the server for example this is sample date which I have to sort based on older to newer date the problem is when I found out sort will... (4 Replies)
Discussion started by: pareshan
4 Replies

9. Shell Programming and Scripting

Change Date from dd-mmm-yyyy to mm/dd/yyyy

I want to change a date from format dd-mmm-yyyy to mm/dd/yyyy. Is there a way to do this with sed or do you have to write a case statement to convert JAN to 01? Thanks (9 Replies)
Discussion started by: stringzz
9 Replies

10. Shell Programming and Scripting

Date Vadidation in YYYY-MM-DD

Date Vadidation in YYYY-MM-DD Hi , Please, Help me in validating given date that is passed in arg as YYYY-MM-DD. It should cover check 1 <= DD <= 31 and 1 <= MM <= 12 for each mm check 1 <=DD<= {28,30,31} (depending) for someYYYY and MM== 02 check 1 <= dd <= 29 I the... (1 Reply)
Discussion started by: itsmehihihi
1 Replies
Login or Register to Ask a Question