The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Grep help flood Shell Programming and Scripting 3 06-06-2008 01:14 AM
Grep Aejaz UNIX for Advanced & Expert Users 3 04-30-2008 07:10 AM
grep dineshr85 Shell Programming and Scripting 1 10-10-2007 04:52 AM
how to exclude the GREP command from GREP yamsin789 UNIX for Advanced & Expert Users 2 10-05-2007 02:59 AM
Make grep -c display like grep -n? Jerrad Shell Programming and Scripting 2 08-25-2006 12:20 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-24-2007
unitipon unitipon is offline
Registered User
  
 

Join Date: May 2007
Posts: 44
How to grep

I am coding a script to grep information in /var/adm/messages which I need to grep the lastest infomation when the script is ran. For example, in /var/adm/messages contains information like this

May 23 17:28:55 stmtrmdbp2 login: [ID 254462 auth.notice] ROOT LOGIN /dev/pts/1 FROM 172.17.128.17
May 23 17:30:30 stmtrmdbp2 login: [ID 254462 auth.notice] ROOT LOGIN /dev/pts/2 FROM 172.17.128.17
May 24 08:42:49 stmtrmdbp2 login: [ID 254462 auth.notice] ROOT LOGIN /dev/pts/1 FROM 172.17.128.25
May 24 08:44:31 stmtrmdbp2 last message repeated 1 time
May 24 09:51:43 stmtrmdbp2 login: [ID 254462 auth.notice] ROOT LOGIN /dev/pts/1 FROM 172.17.128.17
May 24 10:35:13 stmtrmdbp2 login: [ID 254462 auth.notice] ROOT LOGIN /dev/pts/1 FROM 172.17.128.17
May 24 10:40:09 stmtrmdbp2 login: [ID 254462 auth.notice] ROOT LOGIN /dev/pts/2 FROM 172.17.128.17
May 24 10:45:09 stmtrmdbp2 login: [ID 254462 auth.notice] ROOT LOGIN /dev/pts/2 FROM 172.17.128.17


When the script run at May 24 10:46:00, Could it prints info at the last 10 minutes before the script run?
Result should be

May 24 10:40:09 stmtrmdbp2 login: [ID 254462 auth.notice] ROOT LOGIN /dev/pts/2 FROM 172.17.128.17
May 24 10:45:09 stmtrmdbp2 login: [ID 254462 auth.notice] ROOT LOGIN /dev/pts/2 FROM 172.17.128.17


Thank u in Advance
  #2 (permalink)  
Old 05-24-2007
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
Code:
hr=$( date +%H )
min=$( date +%M )
temp=$(( hr * 60 + min -10 ))
nhr=$(( temp / 60 ))
nmin=$(( temp - nhr * 60 ))
awk -F"[ :]" ' $3 >= "'$nhr'" && $4 >= "'$nmin'" ' filename
  #3 (permalink)  
Old 05-24-2007
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,509
Code:
awk 'BEGIN{ 
             tenmin=10*60 
             now=systime()
             date["May"]=5;date["Jan"]=1 #and so on
             "date +%Y" | getline year            
}
{
 n=split($3,t,":") 
 logtime = year" "date[$1]" "$2" "t[1]" "t[2]" "t[3]" 0 0 0"
 a = mktime(logtime)
 if ( (now - a) < tenmin ) { print} 
}' "file"
  #4 (permalink)  
Old 05-24-2007
unitipon unitipon is offline
Registered User
  
 

Join Date: May 2007
Posts: 44
Thank you for your reply but it's not work both

For anbu23 ; I tried to test awk -F"[ :]" ' $3 >= "'$nhr'" && $4 >= "'$nmin'" ' filename by fix value such as awk -F"[ :]" ' $3 >= "10" && $4 >= "46" ' /var/adm/messages but nothing display

For ghostdog74 ; when i run the script that u gave so i found the error
awk: syntax error near line 3
awk: illegal statement near line 3
awk: syntax error near line 5
awk: illegal statement near line 5

Thank for all ur help
  #5 (permalink)  
Old 05-24-2007
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
Quote:
Originally Posted by unitipon
Thank you for your reply but it's not work both

For anbu23 ; I tried to test awk -F"[ :]" ' $3 >= "'$nhr'" && $4 >= "'$nmin'" ' filename by fix value such as awk -F"[ :]" ' $3 >= "10" && $4 >= "46" ' /var/adm/messages but nothing display

For ghostdog74 ; when i run the script that u gave so i found the error
awk: syntax error near line 3
awk: illegal statement near line 3
awk: syntax error near line 5
awk: illegal statement near line 5

Thank for all ur help
Code:
$ cat file
May 23 17:28:55 stmtrmdbp2 login: [ID 254462 auth.notice] ROOT LOGIN /dev/pts/1 FROM 172.17.128.17
May 23 17:30:30 stmtrmdbp2 login: [ID 254462 auth.notice] ROOT LOGIN /dev/pts/2 FROM 172.17.128.17
May 24 08:42:49 stmtrmdbp2 login: [ID 254462 auth.notice] ROOT LOGIN /dev/pts/1 FROM 172.17.128.25
May 24 08:44:31 stmtrmdbp2 last message repeated 1 time
May 24 09:51:43 stmtrmdbp2 login: [ID 254462 auth.notice] ROOT LOGIN /dev/pts/1 FROM 172.17.128.17
May 24 10:35:13 stmtrmdbp2 login: [ID 254462 auth.notice] ROOT LOGIN /dev/pts/1 FROM 172.17.128.17
May 24 10:40:09 stmtrmdbp2 login: [ID 254462 auth.notice] ROOT LOGIN /dev/pts/2 FROM 172.17.128.17
May 24 10:45:09 stmtrmdbp2 login: [ID 254462 auth.notice] ROOT LOGIN /dev/pts/2 FROM 172.17.128.17
$ awk -F"[ :]" ' $3 >= "10" && $4 >= "40" ' file
May 24 10:40:09 stmtrmdbp2 login: [ID 254462 auth.notice] ROOT LOGIN /dev/pts/2 FROM 172.17.128.17
May 24 10:45:09 stmtrmdbp2 login: [ID 254462 auth.notice] ROOT LOGIN /dev/pts/2 FROM 172.17.128.17
Make sue you have entries in your file after time 10:46.
  #6 (permalink)  
Old 05-24-2007
unitipon unitipon is offline
Registered User
  
 

Join Date: May 2007
Posts: 44
Smile Thank

Dear anbu23,

Quote:
I use awk -F" :" ' $3 >= "10" && $4 >= "40" ' file so the result will print out. Thank alot for ur help

When the file has entires liks this

May 24 17:15:12 stmtrmdbp1 su: [ID 366847 auth.info] 'su uusts00' succeeded for root on /dev/???
May 24 17:24:45 stmtrmdbp1 su: [ID 366847 auth.info] 'su oracle' succeeded for root on /dev/???
May 24 17:44:48 stmtrmdbp1 last message repeated 4 times
May 24 18:01:11 stmtrmdbp1 su: [ID 366847 auth.info] 'su xmuser' succeeded for root on /dev/???
May 24 18:11:13 stmtrmdbp1 su: [ID 366847 auth.info] 'su uusts00' succeeded for root on /dev/???
May 24 18:12:45 stmtrmdbp1 su: [ID 366847 auth.info] 'su oracle' succeeded for root on /dev/???
May 24 18:13:47 stmtrmdbp1 last message repeated 1 time
May 24 18:14:11 stmtrmdbp1 su: [ID 366847 auth.info] 'su xmuser' succeeded for root on /dev/???
May 24 18:41:13 stmtrmdbp1 su: [ID 366847 auth.info] 'su uusts00' succeeded for root on /dev/???
May 24 18:42:46 stmtrmdbp1 su: [ID 366847 auth.info] 'su oracle' succeeded for root on /dev/???
May 24 18:46:48 stmtrmdbp1 last message repeated 1 time
May 24 18:47:10 stmtrmdbp1 su: [ID 366847 auth.info] 'su xmuser' succeeded for root on /dev/???
May 24 18:47:12 stmtrmdbp1 su: [ID 366847 auth.info] 'su uusts00' succeeded for root on /dev/???
May 24 18:47:46 stmtrmdbp1 su: [ID 366847 auth.info] 'su oracle' succeeded for root on /dev/???
May 24 18:47:48 stmtrmdbp1 last message repeated 1 time
May 24 18:48:11 stmtrmdbp1 su: [ID 366847 auth.info] 'su xmuser' succeeded for root on /dev/???
May 24 18:48:13 stmtrmdbp1 su: [ID 366847 auth.info] 'su uusts00' succeeded for root on /dev/???
May 24 18:48:44 stmtrmdbp1 su: [ID 366847 auth.info] 'su oracle' succeeded for root on /dev/???


So I tried to awk -F" :" ' $3 >= "18" && $4 >= "40" ' file then the result will show all the 18th hour . how to solve it

Last edited by unitipon; 05-24-2007 at 08:00 AM.. Reason: Misunderstand
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 03:52 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0