I have a requirement to fetch logs of last 'N' days.
I am trying the following command which is working fine if it finds the date of that day in logfile.
START=`TZ="GMT+$((24*N))" date +"%Y %b %d"`
this is being used to fetch 'N'th day's date
and
awk '/'"$START"'/{p=1} /'"$NOW"'/p' <logfile.log
is being used to fetch the records from log file.
like if last 'N' day's date is 2012 Jun 17
and this date is present in file then it gives out the record from 2012 Jun 17 to NOW.
But if last 'N' day's date is 2012 Jun 17 and it is not present in log file then its giving the blank response , though there are records available for date 2012 Jun 16.
Thanks but that is worked in cse if we kknow the exact date or timestamp.
Problem is that if that timestamo or date is not present in log file iit is giving whole file where aas it should give response as per my first post.
Also in your previous reply u suggested that the p=1... so what can i set there.
because if i set p=0 and searching from 2012 Aug 01 to now. Though in log file the data available for 2012 Aug 16 ..but its showing only data of 2012 Aug 30..
Yes this can be done..
Good option, but it giving each time some output... and not sure at what valuue of 'p' there will be an existing date in log file.
So after each execution of command its giving some output.
Yes Raj, this can be done but problem is that we are not sure that at what value of 'p' there will be an existing date in logfile.
So for each value of 'p' command will get executed and hence resulting in some unexpected response.
---------- Post updated at 02:27 AM ---------- Previous update was at 02:21 AM ----------
sample data
:
2012 Aug 01 : sdfjsdfkljsdlfgjlsdfjgldjflgjsdfgasdfg
2012 Aug 05 10:12:dfkjsdlfksdfksdfks;dkf;sdkf;ksdfjsdf
2012 Aug 05 10:13: kfgsdklfjlsedjkflsdjkfljsdfgsda
2012 Aug 30 10:11: sdhfsdkfsdjflsjdfjsdf
2012 Aug 30 10:12: sdhfsdkfsdjflsjdfjsdf
2012 Aug 30 10:12: sdhfsdkfsdjflsjdfjsdf
So if i am asking data of last 30 days it should give whole data from 2012 Aug 01 to 2012 Aug 30.
if last 25 days then:
2012 Aug 05 10:12:dfkjsdlfksdfksdfks;dkf;sdkf;ksdfjsdf
2012 Aug 05 10:13: kfgsdklfjlsedjkflsdjkfljsdfgsda
2012 Aug 30 10:11: sdhfsdkfsdjflsjdfjsdf
2012 Aug 30 10:12: sdhfsdkfsdjflsjdfjsdf
2012 Aug 30 10:12: sdhfsdkfsdjflsjdfjsdf
But if i running it for last 29 days it should give
2012 Aug 05 10:12:dfkjsdlfksdfksdfks;dkf;sdkf;ksdfjsdf
2012 Aug 05 10:13: kfgsdklfjlsedjkflsdjkfljsdfgsda
2012 Aug 30 10:11: sdhfsdkfsdjflsjdfjsdf
2012 Aug 30 10:12: sdhfsdkfsdjflsjdfjsdf
2012 Aug 30 10:12: sdhfsdkfsdjflsjdfjsdf
$cat file
2012 Aug 01 : sdfjsdfkljsdlfgjlsdfjgldjflgjsdfgasdfg
2012 Aug 05 10:12:dfkjsdlfksdfksdfks;dkf;sdkf;ksdfjsdf
2012 Aug 05 10:13: kfgsdklfjlsedjkflsdjkfljsdfgsda
2012 Aug 30 10:11: sdhfsdkfsdjflsjdfjsdf
2012 Aug 30 10:12: sdhfsdkfsdjflsjdfjsdf
2012 Aug 31 10:12: sdhfsdkfsdjflsjdfjsdf
#I don't know how <= is not working for the end date so add one more date to your end date...
$ awk -F ":" '{ if ($1 >= "2012 Aug 01" && $1 <= "2012 Aug 31" ) print }' file
2012 Aug 01 : sdfjsdfkljsdlfgjlsdfjgldjflgjsdfgasdfg
2012 Aug 05 10:12:dfkjsdlfksdfksdfks;dkf;sdkf;ksdfjsdf
2012 Aug 05 10:13: kfgsdklfjlsedjkflsdjkfljsdfgsda
2012 Aug 30 10:11: sdhfsdkfsdjflsjdfjsdf
2012 Aug 30 10:12: sdhfsdkfsdjflsjdfjsdf
$ awk -F ":" '{ if ($1 >= "2012 Aug 01" && $1 <= "2012 Sep 1" ) print }' file
2012 Aug 01 : sdfjsdfkljsdlfgjlsdfjgldjflgjsdfgasdfg
2012 Aug 05 10:12:dfkjsdlfksdfksdfks;dkf;sdkf;ksdfjsdf
2012 Aug 05 10:13: kfgsdklfjlsedjkflsdjkfljsdfgsda
2012 Aug 30 10:11: sdhfsdkfsdjflsjdfjsdf
2012 Aug 30 10:12: sdhfsdkfsdjflsjdfjsdf
2012 Aug 31 10:12: sdhfsdkfsdjflsjdfjsdf
---------- Post updated at 02:50 AM ---------- Previous update was at 02:44 AM ----------
Yes Pamu, i can do this too, good option but in case if date format changed.
will this work?
Specially in awk -F ":" the ":" symbol. beacuse in another file this can be "-" or ";"
-F ":" this used as field separator. If date format changed by that means field separator changed then we have to implement that to the code. But this is not major change you can do it easily.
for START variable..
You can use below code to assign date variable to one day ahead of today's date...