Code to compare date

Hi All

I have a text file (file.txt) which has a date in YYYYMMDD format in the fourth column. I need to compare and check if that date matches the last date of each month in the same format for all the rows.

cat file.txt
1|2|3|20150430
a|b|c|20150430
g|e|f|20150430
.
.

I have been able to figure out the last date of each month. But now I want to match it with with all the rows of fourth column so that by mistake there is no mismatch in any of the rows. I tried this :

awk -F "|" '{print $4}' file.txt > file1.txt

Is there a way using a single awk command to get what I need or a loop after this to match the row(s)?

Your spec is a bit vague and/or incomplete.
So - you have the string to compare to. Where and how? Variable, file, command substitution? What do you want to do if it doesn't match the fourth field of a line in your file? Print the line? An error msg?

Hi RudiC

Thanks for your feedback. Well I actually need to compare the last date of each month with each of the rows of file1.txt
Example :

cat file1.txt
20150430
20150430
20150430
20150430
20150429
.
.
..

and I already have the last date of the month as :

20150430

Now I need to compare that each and every row of file1.txt matches the above date i.e.

20150430

and if there is a mismatch (as shown in file1 in 5th row where the date is

20150429

it will print that row saying :"Date Mismatch".

Well, your second sample differs from your first one; let's assume you mean the first one's structure. And, as you didn't say how you provide the comparison string, try and adapt

awk -F "|" -vCOMP='20150430' '$4 != COMP {print $0 ": Date mismatch!"}' file
g|e|f|20150429: Date mismatch!
1 Like

Hi RudiC

awk "usage" is not working. Erroring out

WHAT?

medpili1: awk -F"|" -vCOMP'20150430' '$4 != COMP {print $0 ": Date Mismatch!"}' file.txt 
Usage: awk [-u] [-F Character][-v Assignment][-f File|Commands][Assignment|File] ...

No idea. Its working both on linux and FreeBSD in here. Try a space between -v and COMP.

Nope its not. I am using AIX with korn shell.

-vCOMP='20150430'

2 Likes

Didn't see that. HOW could it disappear?

Hi CarloM/RudiC

Thanks for your correction. The command is working now :slight_smile: