UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #1  
Old November 9th, 2004, 03:35 PM
Redtailed's Avatar
Redtailed Redtailed is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Location: Utah, USA
Posts: 83 Redtailed User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 m 31 sec
Reputation Power: 5
Grep Regular Expression to Filter Text File

I have a plain text log file on a *nix server that I'd like to be able to view errors as they occur for troubleshooting purposes. The problem is that the log also shows non-errors and errors that I don't care about (error: general).

I did the following:
Code:
tail -f logfile.log | grep error


This worked well in showing me the errors as they occurred but there are a lot of lines with "error" that I don't want to show up.

If I do:
Code:
tail -f logfile.log | grep -v general


I exclude all the error: general lines but now have all the non-error lines as well. I need to combine the criteria for grep but havent' found a way to do it.
Code:
grep logfile.log error | grep -v general

The above works, but it doesn't take advantage of the tail -f command for viewing in real time.

Any gurus who know how to combine these statements?
__________________
- Redtailed

Reply With Quote
  #2  
Old November 10th, 2004, 12:39 AM
guggach guggach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2004
Location: Middle Europa
Posts: 1,083 guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 Days 19 h 49 m 57 sec
Reputation Power: 9
tail -f logfile.log | grep -v general

i never tryed that, imo does not work, because grep is waiting for
input, and as long tail is running, it gets nothing.

Reply With Quote
  #3  
Old November 10th, 2004, 09:24 AM
Perderabo Perderabo is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 121 Perderabo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 m 54 sec
Reputation Power: 5
Try:
tail -f error.log | grep error | grep -v general

Reply With Quote
  #4  
Old November 10th, 2004, 09:44 AM
Redtailed's Avatar
Redtailed Redtailed is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Location: Utah, USA
Posts: 83 Redtailed User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 m 31 sec
Reputation Power: 5
Quote:
Originally Posted by Perderabo
Try:
tail -f error.log | grep error | grep -v general

I did in fact try this, grep twice with pipes connecting them. There is no output whatsoever with this method, for reasons I don't understand. As long as I pipe to grep once, it works (whether I invert the results or not with '-v' doesn't matter).

Incidentally I am running FreeBSD 4.10 and GNU grep 2.4.1 (as far as I can tell). (How do you easily tell the version of apps like grep and awk?)

Reply With Quote
  #5  
Old November 10th, 2004, 05:05 PM
jim mcnamara jim mcnamara is offline
......@.........
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jun 2004
Posts: 1,307 jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 4 h 28 m 57 sec
Reputation Power: 48
what /usr/bin/awk

If you do not know the path to awk or whatever file it is try:
what `which awk`

Some systems have the ident command.

This only works if the development team followed cvs standards for file headers.

Reply With Quote
  #6  
Old November 10th, 2004, 05:29 PM
Redtailed's Avatar
Redtailed Redtailed is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Location: Utah, USA
Posts: 83 Redtailed User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 m 31 sec
Reputation Power: 5
Thanks for the version-finding info. Every now and then someone wants the version to some utility program and I generally had no answers.

Reply With Quote
  #7  
Old November 11th, 2004, 10:00 AM
Perderabo Perderabo is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 121 Perderabo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 m 54 sec
Reputation Power: 5
Not sure what shell you have, but this will work with any posix shell like bash or ksh...

Code:
#! /usr/bin/bash

tail -f file | while read line ; do
       [[ $line != *error* ]] && continue
       [[ $line = *general* ]] && continue
       echo "$line" > /dev/tty
done
exit 0

Reply With Quote
  #8  
Old November 11th, 2004, 03:10 PM
Redtailed's Avatar
Redtailed Redtailed is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Location: Utah, USA
Posts: 83 Redtailed User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 m 31 sec
Reputation Power: 5
I was running tcsh but I quickly installed bash (GNU 2.05b) and gave it a try. Under tcsh it complained about the [['s. Under bash, I am experimenting with it to get the right output. It seems to be working great.

Thanks for the help! The little shell script seems to be just the ticket.

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > Grep Regular Expression to Filter Text File


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway