Code to compare date


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Code to compare date
# 1  
Old 05-28-2015
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.
Code:
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 :
Code:
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)?

Last edited by rbatte1; 05-28-2015 at 08:57 AM.. Reason: Added ICODE tags and tightened CODE tags
# 2  
Old 05-28-2015
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?
# 3  
Old 05-28-2015
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 :
Code:
cat file1.txt
20150430
20150430
20150430
20150430
20150429
.
.
..

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

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

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

it will print that row saying :"Date Mismatch".
# 4  
Old 05-28-2015
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
Code:
awk -F "|" -vCOMP='20150430' '$4 != COMP {print $0 ": Date mismatch!"}' file
g|e|f|20150429: Date mismatch!

This User Gave Thanks to RudiC For This Post:
# 5  
Old 05-28-2015
Hi RudiC

awk "usage" is not working. Erroring out
# 6  
Old 05-28-2015
WHAT?
# 7  
Old 05-28-2015
Code:
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] ...

Moderator's Comments:
Mod Comment Note that if you continue to ignore forum rules (including using appropriate CODE tags), you are likely to be banned from posting on this site.

Last edited by Don Cragun; 05-28-2015 at 04:10 PM.. Reason: Add CODE tags.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Compare Date to today's date in shell script

Hi Community! Following on from this code in another thread: #!/bin/bash file_string=`/bin/cat date.txt | /usr/bin/awk '{print $5,$4,$7,$6,$8}'` file_date=`/bin/date -d "$file_string"` file_epoch=`/bin/date -d "$file_string" +%s` now_epoch=`/bin/date +%s` if then #let... (2 Replies)
Discussion started by: Greenage
2 Replies

2. Answers to Frequently Asked Questions

Compare date in .txt with system date and remove if it's lesser than system date

I m working on shell scripting and I m stuck where in my .txt file there is column as expiry date and I need to compare that date with system date and need to remove all the rows where expiry date is less than system date and create a new .txt with update. (1 Reply)
Discussion started by: Stuti
1 Replies

3. UNIX for Beginners Questions & Answers

Compare date in .txt with system date and remove if it's lesser than system date

Can someone help me with the code wherein there is a file f1.txt with different column and 34 column have expiry date and I need to get that and compare with system date and if expiry date is <system date remove those rows and other rows should be moved to new file f2.txt . I don't want to delete... (2 Replies)
Discussion started by: Stuti
2 Replies

4. Shell Programming and Scripting

Compare the system date with date from a text file

I get the date that's inside a text file and assigned it to a variable. When I grep the date from the file, I get this, Not After : Jul 28 14:09:57 2017 GMT So I only crop out the date, with this command echo $dateFile | cut -d ':' -f 2,4The result would be Jul 28 14:57 2017 GMT How do I... (3 Replies)
Discussion started by: Loc
3 Replies

5. Shell Programming and Scripting

Shell script to compare two files of todays date and yesterday's date

hi all, How to compare two files whether they are same are not...? like i had my input files as 20141201_file.txt and 20141130_file2.txt how to compare the above files based on date .. like todays file and yesterdays file...? (4 Replies)
Discussion started by: hemanthsaikumar
4 Replies

6. Shell Programming and Scripting

Compare Date with String in date format

Hi, I am new to this forum, searched, but for some reason did not find much help, so a new thread. I am trying to compare date with a string which is in date format , but for some reason, system does not give me the right result. Date is coming in a file and i am comparing the same with... (2 Replies)
Discussion started by: mkstool
2 Replies

7. Shell Programming and Scripting

Adding days to system date then compare to a date

Hi! I am trying to read a file and every line has a specific date as one of its fields. I want to take that date and compare it to the date today plus 6 days. while read line do date=substr($line, $datepos, 8) #date is expected to be YYYYMMDD if ; then ...proceed commands ... (1 Reply)
Discussion started by: kokoro
1 Replies

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

9. Shell Programming and Scripting

Compare Start date and End date...

Hi All, I have problem in my file. It has two date variable. There are 2 variables which has the values as below 1. START_MONTH = “Date(YYYYMM) format” Ex: 201008 2. END_MONTH = “Date(YYYYMM) format” Ex: 201105 The end date should be greater than start date. Now we... (3 Replies)
Discussion started by: suresh01_apk
3 Replies

10. Shell Programming and Scripting

Compare date from db2 table to yesterday's Unix system date

I am currently running the following Korn shell script which works fine: #!/usr/bin/ksh count=`db2 -x "select count(*) from schema.tablename"` echo "count" I would like to add a "where" clause to the 2nd line that would allow me to get a record count of all the records from schema.tablename... (9 Replies)
Discussion started by: sasaliasim
9 Replies
Login or Register to Ask a Question