|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
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
|
||||
|
||||
|
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 |
|
#2
|
|||
|
|||
|
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. |
|
#3
|
|||
|
|||
|
Try:
tail -f error.log | grep error | grep -v general |
|
#4
|
||||
|
||||
|
Quote:
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?) |
|
#5
|
|||
|
|||
|
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. |
|
#6
|
||||
|
||||
|
Thanks for the version-finding info. Every now and then someone wants the version to some utility program and I generally had no answers.
|
|
#7
|
|||
|
|||
|
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
|
|
#8
|
||||
|
||||
|
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. |
![]() |
| Viewing: Dev Shed Forums > Operating Systems > UNIX Help > Grep Regular Expression to Filter Text File |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|