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
# 8  
Old 11-20-2014
i've decided to:

remove 60days from the current time.
scan the data file
if a chunk does not contain "entry_time" and "comment_data", output it!
if a chunk actually contains "entry_time" and "comment_data", output it ONLY if the entry time is greater than $LEAST

the script below is my attempt. but its not working. can someone please help me modify it. i feel like im very close. i just need to be able to grab the value for entry_time and do the comparison:

Code:
NOW=$(date +%s)

LEAST=$(awk "BEGIN{ print $NOW - 5443200}")

FILE=${1}

awk -v TPASSED=$LEAST -v NOWTIME=$NOW -v pattern1="entry_time" -v pattern2="comment_data" '
    BEGIN {
        RS = "{";
        FS = "{";
    }
    FNR == 1 {
        record_sep = RT;
    }
    {
        for (i = 1; i <= NF; i++ ) {
            if (( match( $i, pattern1 ) > 0 ) && ( match( $i, pattern2 ) > 0 ))
                    { print record_sep , $0; break; }
            S[$record_sep]

#  for(X in S) delete S[X];
#
#  for(N=2; N<=NF; N++)
#  {
#       gsub(/^[ \t]+/, "", $N);
#       split($N, A, "=");
#       D[A[1]] = A[2]
#       i = 3;
#       while (i in A)
#          D[A[1]] = D[A[1]] "=" A[i++];
#  }

            else
                    { print record_sep , $0; break; }
        }
    }
' ${FILE}


Last edited by SkySmart; 11-21-2014 at 12:24 AM..
# 9  
Old 11-21-2014
Try this:

Code:
awk -v pattern1="entry_time" -v pattern2="comment_data" '
    BEGIN {
        srand(); now=srand();
        least=now-5443200;
        RS=""
        FS="="
        ORS="\n\n"
    }
    {
      p=t=0
      for(i=1;i<=NF;i++)  {
        j=i+1
        if($i==pattern2) p=1
        if($i==pattern1) t=$j
      }
      if((t==0 && p==0) || (t>least && p==1))
        print
    }
' $1

This User Gave Thanks to jlliagre For This Post:
# 10  
Old 11-21-2014
Not sure if this is too simplistic:
Code:
awk     'BEGIN                          {NOW=srand()}
         /comment_data/ &&
         match ($0,/entry_time=[0-9]*/) {if (NOW-substr ($0, RSTART+11,  RLENGTH-11) > 5184000) next}
         1
        ' RS= ORS="\n\n" file

BTW - 5443200 represents 63 days.
This User Gave Thanks to RudiC For This Post:
# 11  
Old 11-21-2014
Quote:
Originally Posted by RudiC
Not sure if this is too simplistic:
Code:
awk     'BEGIN                          {NOW=srand()}
         /comment_data/ &&
         match ($0,/entry_time=[0-9]*/) {if (NOW-substr ($0, RSTART+11,  RLENGTH-11) > 5184000) next}
         1
        ' RS= ORS="\n\n" file

BTW - 5443200 represents 63 days.
this is hanging when i run it. i get no output at all. it just hangs.

---------- Post updated at 10:34 AM ---------- Previous update was at 10:32 AM ----------

Quote:
Originally Posted by jlliagre
Try this:

Code:
awk -v pattern1="entry_time" -v pattern2="comment_data" '
    BEGIN {
        srand(); now=srand();
        least=now-5443200;
        RS=""
        FS="="
        ORS="\n\n"
    }
    {
      p=t=0
      for(i=1;i<=NF;i++)  {
        j=i+1
        if($i==pattern2) p=1
        if($i==pattern1) t=$j
      }
      if((t==0 && p==0) || (t>least && p==1))
        print
    }
' $1

same thing here. it seems to just hang. looks like we're getting closer Smilie
# 12  
Old 11-21-2014
Did you supply YOUR file name?
# 13  
Old 11-21-2014
Quote:
Originally Posted by RudiC
Did you supply YOUR file name?
Yes I did. i replaced the "$1" with my file.

and for the code from jilla, i replaced 'file' with my datafile.
# 14  
Old 11-21-2014
That "hanging" is typical for awk reading from your terminal, waiting for you typing input, which it does when no file name is given. Try setting the -v and or -x options and run it again.

Using your input file from post#1 with a modified first "chunk", I get
Code:
awk     'BEGIN                          {LIMDAT=srand() - 5184000}
         /comment_data/ &&
         match ($0,/entry_time=[0-9]*/) {if (substr ($0, RSTART+11,  RLENGTH-11) < LIMDAT) next}
         1
        ' RS= ORS="\n\n" file
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
}

This User Gave Thanks to RudiC 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