Printing from file with awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Printing from file with awk
# 1  
Old 03-17-2014
Printing from file with awk

Hi,

I'm trying to find an efficient way with awk to print the lines from a file which match the string 'OFFLINE' and DO NOT match the string ONLINE within 2 minutes from the date stamp in column 1.

The file is similar to the following:

Code:
09:40:26    HostC    OFFLINE
09:40:43    HostA    ONLINE
09:41:15    HostB    OFFLINE
09:41:55    HostA    ONLINE
09:42:26    HostD    ONLINE
09:42:49    HostB    ONLINE
09:44:05    HostB    ONLINE
09:44:59    HostB    OFFLINE

The output I want from this file would be:

Code:
09:40:26    HostC    OFFLINE

Is this something that can be achieved with AWK?

Many thanks,
# 2  
Old 03-17-2014
Try this:
Code:
awk '{ split($1,A,":");  T=(((A[1]*60)+A[2])*60)+A[3] }
/OFFLINE/ { P=T }
/ONLINE/ { P="" }
(T-P) > 120 { print }' inputfile

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 03-17-2014
Few modifications in Corona688's solution:
Code:
awk '
        /OFFLINE/ {
                split ( $1, A, ":" )
                T = ( ( ( A[1] * 60 ) + A[2] ) * 60 ) + A[3]
                if ( ( T - P ) < 120 && R )
                        print R
                P = T
                R = $0
        }
' file

# 4  
Old 03-17-2014
Thanks for the quick reply!

Unfortunately that outputs the following:

Code:
09:40:43    HostA    ONLINE
09:41:55    HostA    ONLINE
09:42:26    HostD    ONLINE
09:42:49    HostB    ONLINE
09:44:05    HostB    ONLINE

I'll see if I can alter it a bit.

---------- Post updated at 12:34 PM ---------- Previous update was at 12:33 PM ----------

Thanks Yoda, I'll try :-)
# 5  
Old 03-18-2014
python

Code:
d={}
td={}
with open("a.txt") as file:
 for line in file:
  line=line.replace("\n","")
  items=line.split(" ")
  a=[int(i) for i in items[0].split(":")]
  t=a[0]*3600+a[1]*60+a[2]
  if items[-1]=='OFFLINE':
   d[items[-2]]=t
  elif items[-1]=='ONLINE' and items[-2] in d:
   gap = t - d[items[-2]]
   if gap<120:
    td[items[-2]]=1
    
for i in d:
 if i not in td:
   print(d[i]//3600,":",(d[i]%3600)//60,":",(d[i]%3600)%60,' ',i,' OFFLINE',sep="")

# 6  
Old 03-18-2014
Yoda

For some reason it doesn't look like awk is looking to see if a line with ONLINE appears within 2 mins.

I can see that the code is printing R if T - P is less than 120 seconds but I'm a bit confused as to what P actually is??

Many thanks,

---------- Post updated at 06:00 AM ---------- Previous update was at 05:11 AM ----------

Using the following input file:

Code:
09:40:26    HostC    OFFLINE
09:40:43    HostA    ONLINE
09:41:15    HostB    OFFLINE
09:41:16    HostB    ONLINE
09:41:55    HostA    OFFLINE
09:42:26    HostD    ONLINE

the following awk code:

Code:
awk '
     	/OFFLINE/ {
                split ( $1, A, ":" )
                T = ( ( ( A[1] * 60 ) + A[2] ) * 60 ) + A[3]
                if ( ( T - P ) < 120 && R )
                        print R
                P = T
                R = $0
        }
' inputfile

Outputs the following:

Code:
09:40:26    HostC    OFFLINE
09:41:15    HostB    OFFLINE

HostB shouldn't be listed in the output because it is ONLINE within 2 mins of being OFFLINE.

I'm a bit confused as to where awk is interrogating the file to find the string ONLINE.

Thanks,
# 7  
Old 03-18-2014
I would choose another python approach by using datetime calculation facilities:
Code:
#cat filter.py
#!/usr/bin/python

import sys
import datetime

first_offline = {}
with  open(sys.argv[1],'r') as f:
    for line in f:
        (dt, host, status) = line.split()
        dt = datetime.datetime.strptime(dt,"%H:%M:%S")
        if status == 'OFFLINE' and host not in first_offline:
            first_offline[host] = (dt,line)
        elif status == 'ONLINE' and host in first_offline:
            dif_time = abs(dt - first_offline[host][0])
            if dif_time.seconds + dif_time.days * 24 * 3600 < 120:
                del first_offline[host]

if first_offline[host][1] == line:
    del first_offline[host]

for line in first_offline.values():
    print line[1],

Usage:
Code:
./filter.py infile

This User Gave Thanks to Klashxx For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk to clean up input file, printing both fields

In the f1 file below I am trying to clean it up removing lines the have _tn_ in them. Next, removing the characters in $2 before the ninth /. Then I remove the ID_(digit- always 4). Finally, the charcters after and including the first _. It is curently doing most of it but the cut is removing $1... (5 Replies)
Discussion started by: cmccabe
5 Replies

2. Shell Programming and Scripting

Printing string from last field of the nth line of file to start (or end) of each line (awk I think)

My file (the output of an experiment) starts off looking like this, _____________________________________________________________ Subjects incorporated to date: 001 Data file started on machine PKSHS260-05CP ********************************************************************** Subject 1,... (9 Replies)
Discussion started by: samonl
9 Replies

3. Shell Programming and Scripting

[awk] printing value of a variable assignment from a file

Heyas Me try to print only the value of a (specific) variable assignment from a file. What i get (1): :) tui $ bin/tui-conf-get ~/.tui_rc TUI_THEME dot-blue "" "$TUI_DIR_INSTALL_ROOT/usr" "$TUI_DIR_INSTALL_ROOT/etc/tui" "$TUI_PREFIX/share/doc/tui" "$TUI_PREFIX/share/tui"... (2 Replies)
Discussion started by: sea
2 Replies

4. Shell Programming and Scripting

Awk: Comparing arguments with in line values of file and printing the result

I need to develop a script where I will take two date arguments as parameter date1 and date2 which will in format YYYYMM. Below is the input file say sample.txt. sample.txt will have certain blocks starting with P1. Each block will have a value 118,1:TIMESTAMP. I need to compare the... (7 Replies)
Discussion started by: garvit184
7 Replies

5. Shell Programming and Scripting

awk - printing the passwd file

I've got a number of RHEL systems and I'm trying to use awk to read and format the output of /etc/passwd. But I'd like to display the host name of the system at the beginning of each line of output. I've got it working without the adding the host name in this script: #!/bin/bash ... (3 Replies)
Discussion started by: westmoreland
3 Replies

6. Shell Programming and Scripting

awk printing only parts of file

I am afraid I don't understand awk well enough to do the following. I have a file with a bunch of select statements where the a line starts off with this pattern: "Last parsed SQL statement :", then continues with the select statement. At the first blank space I'd like it to stop, print that... (5 Replies)
Discussion started by: fwellers
5 Replies

7. UNIX for Dummies Questions & Answers

Printing File Contents Using AWK

I want to print the contents of a file from 2nd line to last but one line using the AWK command. I tried using the Shell Script,MyScript as fallows: MyScript: { if(NR>1) { if(NR<9) { print $0 } } } and used the commnd : awk -f MyScript Filename Note: Filename contains 9 lines text.... (0 Replies)
Discussion started by: ashok.g
0 Replies

8. Shell Programming and Scripting

AWK printing

i have a file containing a line 123456 is it possible to use AWK to print it out to look like 1 2 3 4 5 6 (8 Replies)
Discussion started by: tomjones
8 Replies

9. Shell Programming and Scripting

need help with awk in printing the fields in a file

hi all i get a file from a server and i do not know how many fields that file will contain. i need to cut from the second column of the file to the last, irrespective of how many fields are there. is there any way to make the awk command dynamic for fetching from the second to the last... (4 Replies)
Discussion started by: sais
4 Replies

10. Shell Programming and Scripting

AWK - printing certain fields when field order changes in data file

I'm hoping someone can help me on this. I have a data file that greatly simplified might look like this: sec;src;dst;proto 421;10.10.10.1;10.10.10.2;tcp 426;10.10.10.3;10.10.10.4;udp 442;10.10.10.5;10.10.10.6;tcp sec;src;fac;dst;proto 521;10.10.10.1;ab;10.10.10.2;tcp... (3 Replies)
Discussion started by: eric4
3 Replies
Login or Register to Ask a Question