Grep search for value between dates


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep search for value between dates
# 1  
Old 11-28-2012
Grep search for value between dates

Hi
I am new to scripting and I have a requirement to grep a value from large numbers of xml files and see if this value exist then I want to check the date and see if it falls between two dates (eg: today and feb 17), then print the name of file. the values in xml file is as follow
Name="TNT_License_End" Value="2012-12-29"
first check for TNT_Licencse_End then for the date if it actually between today and Feb 17.
Thank you in anticipation for you help.
# 2  
Old 11-28-2012
grep does not understand dates, it is not a programming language. awk is a programming language, and better suited.

You are lucky, the dates are in YYYY-MM-DD order, which is directly sortable and comparable with > < string comparisons.

We probably need to see more of the XML.

Last edited by Corona688; 11-28-2012 at 02:06 PM..
# 3  
Old 11-28-2012
This might work, depending on your XML.

Code:
$ awk -v FIRST="2012-12-28" -v LAST="2012-12-31" '
                (NR==1) { F=FILENAME }
                F!=FILENAME { if(M) print F; F=FILENAME ; M=0 }
                /TNT_License_End/ && match($0, "Value=\"[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]\"") {
                        V=substr($0,RSTART+7,RLENGTH-8)
                        if((V <= LAST)&&(V >= FIRST)) M=1 }
                END { if(M) print F }' input1.xml input2.xml

input1.xml

$

# 4  
Old 11-28-2012
Code:
#!/bin/bash

CURR_DATE=$( date +"%s" )
PREV_DATE=$( date -d"2012-02-17" +"%s" )                

extr_xmld=$( awk -F"Value=" '/TNT_License_End/ { gsub("\"","",$2); print $2; } ' xml_file )
XMLF_DATE=$( date -d"$extr_xmld" +"%s" )

if [ ${XMLF_DATE} -ge ${PREV_DATE} ] && [ ${XMLF_DATE} -le ${CURR_DATE} ]
then
        echo "Is between Feb 17 & Today"
else
        echo "Is not between Feb 17 & Today"
fi

# 5  
Old 11-28-2012
Like I said, YYYY-MM-DD dates are directly sortable as strings. There's no need to convert them to epoch seconds.
# 6  
Old 11-28-2012
thanks alot for your prompt response, I think the code is very close to what I expect but the date I mean is going to end on feb 17th of 2013, also there is an error -ge: unary operator expected.
thank you again for your help
# 7  
Old 11-28-2012
Looks like XMLF_DATE is not set, perform below changes:-
Code:
#!/bin/bash

CURR_DATE=$( date +"%s" )
PREV_DATE=$( date -d"2012-02-17" +"%s" )                

extr_xmld=$( awk -F"Value=" '/TNT_License_End/ { gsub("\"","",$2); print $2; } ' xml_file )
if [ ! -z $extr_xmld ]
then
 XMLF_DATE=$( date -d"$extr_xmld" +"%s" )

 if [ ${XMLF_DATE} -ge ${PREV_DATE} ] && [ ${XMLF_DATE} -le ${CURR_DATE} ]
 then
        echo "Is between Feb 17 & Today"
 else
        echo "Is not between Feb 17 & Today"
 fi
fi

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to use a grep search to search for a specific string within multiple directories?

Lets say I have a massive directory which is filled with other directories all filled with different c++ scripts and I want a listing of all the scripts that contain the string: "this string". Is there a way to use a grep search for that? I tried: grep -lr "this string" * but I do not... (3 Replies)
Discussion started by: Circuits
3 Replies

2. What is on Your Mind?

Grep file containing dates

How to grep a file containing dates to only last 30 days then move to another folder (7 Replies)
Discussion started by: kmarcus
7 Replies

3. Programming

MYSQL query search between dates

Just a little help if possible I have a table with date data, I want to select dates that fall between today and 1 month ago, here's my query SELECT id, stdate, DATE_SUB(CURDATE(), INTERVAL 1 month) as monthago, CURDATE() as today FROM data_table where (stdate between 'today' and 'monthago')... (4 Replies)
Discussion started by: barrydocks
4 Replies

4. Shell Programming and Scripting

Search pattern on logfile and search for day/dates and skip duplicate lines if any

Hi, I've written a script to search for an Oracle ORA- error on a log file, print that line and the .trc file associated with it as well as the dateline of when I assumed the error occured. In most it is the first dateline previous to the error. Unfortunately, this is not a fool proof script.... (2 Replies)
Discussion started by: newbie_01
2 Replies

5. Shell Programming and Scripting

Script to search specific folders dates /mm/dd/ structure

Hi, I have a script that handles a huge amount of log files from many machines and copies it into a SAN location with the following directory structure: /SAN/machinenames/yyyy/m/d so for example /SAN/hosta/2011/3/12/files* Now I am writing a bash script to search for files between to date... (4 Replies)
Discussion started by: GermanJulian
4 Replies

6. Shell Programming and Scripting

Need script to generate all the dates in DDMMYY format between 2 dates

Hello friends, I am looking for a script or method that can display all the dates between any 2 given dates. Input: Date 1 290109 Date 2 010209 Output: 300109 310109 Please help me. Thanks. :):confused: (2 Replies)
Discussion started by: frozensmilz
2 Replies

7. UNIX for Dummies Questions & Answers

How do I search between dates?

I have a folder that has files that go way back to 2005. How can I search for files between say Jan 2006 - Dec 2007 only? I did a man on the Find command but nothing in there. Any help would be appreciated. (4 Replies)
Discussion started by: bbbngowc
4 Replies

8. Shell Programming and Scripting

grep a log file to filter previous dates

Hi, I have problem of filtering a log file from my perl script. #cat /data/pinpe.csv_20070731 | nawk -v FS=, '{print $1','$18','$22','$26}' | grep -w 100 | grep -w 1 | nawk '{print $4}' Below is the output: 2009-06-16 2009-01-29 2009-06-02 2008-03-05 2007-08-05 2007-09-24... (5 Replies)
Discussion started by: pinpe
5 Replies

9. Shell Programming and Scripting

grep a log file between 2 dates

Hi Currently I can grep a log file with the following command: $results = `grep -A 2 '^$date.$time.*' $log`; and the following arguments: $date = 2007/04/25 $time = 16:07 Log example: 2007/04/25 16:07:12.145701 2007/05/25 14:07:12.145701 2007/05/25 17:07:12.145701 2007/06/25... (37 Replies)
Discussion started by: Epiphone
37 Replies
Login or Register to Ask a Question