awk to calculate date and show data


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk to calculate date and show data
# 1  
Old 11-20-2014
awk to calculate date and show data

data:
Code:
hostcomment {
host_name=myhost01
entry_type=1
comment_id=1
source=0
persistent=1
entry_time=1415723753
expires=0
expire_time=0
author=hpsm
comment_data=IM0837437472
}

program {
modified_host_attributes=1
modified_service_attributes=1
enable_notifications=1
active_service_checks_enabled=1
passive_service_checks_enabled=1
active_host_checks_enabled=1
passive_host_checks_enabled=1
enable_event_handlers=1
obsess_over_services=0
obsess_over_hosts=0
check_service_freshness=1
check_host_freshness=0
enable_flap_detection=1
enable_failure_prediction=1
process_performance_data=0
global_host_event_handler=
global_service_event_handler=
next_comment_id=177361
next_downtime_id=1039
next_event_id=418881
next_problem_id=194777
next_notification_id=59919
}

hostcomment {
host_name=myhost01
entry_type=1
comment_id=1
source=0
persistent=1
entry_time=1415723753
expires=0
expire_time=0
author=hpsm
comment_data=IM023434343
}

i have a file that contains chunks similar to the above.

i want to parse this file and skip over chunks that have entry time which is older than 60 days. Meaning, DO NOT output those chunks.

Please note, some of the chunks in the data file do not have "entry_time". so for those entries, i want this script to output the chunk and move on to the next chunk.

So in other words, i want to do the date subtraction only on chunks that have "entry_time".


this is the code i'm using:
Code:
NOW=$(date +%s)

FILE=${1}

awk -v NOWTIME=$NOW -v mac="=" '
    BEGIN { 
        RS = "{"; 
        FS = " "; 
    } 
    FNR == 1 {
        record_sep = RT;
    }
    { 

        for (i = 1; i <= NF; i++ ) { 
            if ( match( $i, mac ) > 0 ) {
                print record_sep , $0;
                break;
            }
        } 
    }
' ${FILE}

how do i substract the entry time from the time now??? and also, is this script good enough to do what i need?

Last edited by SkySmart; 11-20-2014 at 07:34 PM..
# 2  
Old 11-20-2014
IMO you are better off using perl for this problem...the "entry_time" is given in seconds since the *nix Epoch...and if your awk can figure out the current Epoch then you'd be able to subtract the "now" Epoch from the "entry_time" Epoch and dole out the desired chunks to an output file.

perl comes with the date and time routines builtin...so if I were you I'd doing this in perl otherwise feel free to ignore this post...
# 3  
Old 11-20-2014
Quote:
Originally Posted by shamrock
IMO you are better off using perl for this problem...the "entry_time" is given in seconds since the *nix Epoch...and if your awk can figure out the current Epoch then you'd be able to subtract the "now" Epoch from the "entry_time" Epoch and dole out the desired chunks to an output file.

perl comes with the date and time routines builtin...so if I were you I'd doing this in perl otherwise feel free to ignore this post...
unfortunately, i cant write in a language that i'm unfamiliar with. if this can be done in perl, please, if you can, supply the perl code.

im pretty sure theres goto be a way around this with awk.
# 4  
Old 11-20-2014
That'd depend on what system you are on ie your OS...because if you are using gawk then you certainly can...just look up the systime function inside the gawk man page...
# 5  
Old 11-20-2014
its a linux red hat host.
# 6  
Old 11-20-2014
Quote:
Originally Posted by SkySmart
its a linux red hat host.
Then you probably have gawk so "man gawk" and search for "systime"...
# 7  
Old 11-20-2014
Quote:
Originally Posted by shamrock
if your awk can figure out the current Epoch then you'd be able to subtract the "now" Epoch from the "entry_time" Epoch and dole out the desired chunks to an output file.
Although it is not a well known fact, all awk releases are able to retrieve the current epoch time by standard :
Code:
$ awk 'BEGIN {srand();time=srand();print time}'
1416532245

These 2 Users Gave Thanks to jlliagre For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Linux

How to calculate the quarter end date according to the current date in shell script?

Hi, My question is how to calculate the quarter end date according to the current date in shell script? (2 Replies)
Discussion started by: Divya_1234
2 Replies

2. Shell Programming and Scripting

Calculate Julian date of a given date

How to get Julian date (Three digit) of a given date (Not current date)? I do not have root privilege - so can not use date -d. Assume that we have three variables year, month and date. Thx (5 Replies)
Discussion started by: Soham
5 Replies

3. Shell Programming and Scripting

Calculate date

Hello, i need calculate the day before in this format aammdd, for example if i have 110701 i need a script to return the value 110630. I only know what with this comand "date %y%m%d" return the date system. Sorry for my english, it is very bad, i know... Thank you very much Grettings! (1 Reply)
Discussion started by: Xedrox
1 Replies

4. Shell Programming and Scripting

Using awk to rearrange data according to date

Hi, I have a large data frame as shown below, where data is separated into years. 10 May 2011 Created: 10 May 11 15:05 GMT Scale: SIO-2005 and others GC-MD, Cape Grim, Tasmania, Lat.: 40.68S, Lon.: 144.69E, Alt: 94m above sea level You can use the following format in Fortran to read data... (4 Replies)
Discussion started by: gd9629
4 Replies

5. Shell Programming and Scripting

How to show last month date?

Hi Guys, Please somebody give me a hand to show the Month & Year in assigning to a variable and with format "MMMYY" (i.e. Jul11). See below preferred output. I tried using this but it is giving me the current month... date = "`date +%b%y`" Aug11 DESIRED OUTPUT: Jul11 ... (3 Replies)
Discussion started by: pinpe
3 Replies

6. Shell Programming and Scripting

Calculate data and make it into new column using awk

Hi everyone, just some simple question... i've been using a awk script to calculate my data... i have 3 files: file a1.txt: 2 3 4 5 3 4 file a2.txt: 4 5 6 7 8 (1 Reply)
Discussion started by: yat
1 Replies

7. Shell Programming and Scripting

calculate the average of time series data using AWK

Hi, I have two time series data (below) merged into a file. t1 and t2 are in unit of second I want to calculate the average of V1 every second and count how many times "1" in V2 is occur within a second Input File: t1 V1 t2 V2 10.000000... (5 Replies)
Discussion started by: nica
5 Replies

8. UNIX for Dummies Questions & Answers

ls how to not show date

Hello, I'm using ls -laR to print out a list of file and folders. I want to print only the permission, file size and file name. Also, excluding the '.' and '..'. result from ls -laR: total 6 drwxr-xr-x 8 user staff 512 Nov 28 16:17 . drwxr-x--- 16 user staff 1024... (3 Replies)
Discussion started by: minifish
3 Replies

9. Shell Programming and Scripting

calculate the date of next satureday of current date.

I want to calculate the date of next satureday of current date using shell script. Suppose, today is 27-feb-08 I want to get the date of next satureday, which means 01-mar-08, in the formate '' YYMMDD ". I do this in ksh.. Please tell me any type of command which help me out. Thanks in... (3 Replies)
Discussion started by: rinku
3 Replies

10. Shell Programming and Scripting

How to show yesterday date

HI all, i am a junior learner, can u teach me how to show yesterday time with unix command? thanks! Cloud (1 Reply)
Discussion started by: wind_n_cloud
1 Replies
Login or Register to Ask a Question