best html web page development

USING MULTIPLE grep AFTER tail -f in UNIX

BY ALI ULVI TALIPOGLU

We use tail -f when following log files continuously. But after tail -f we can not use multiple pipes like tail -f | grep word1 | grep -v word2 due to unix kernel's 'feature'. So instead you can use "regular-expression lookahead" for example when you want to see only the lines that contain words error or :E: but not contain words connect and CDR as below:

tail -f log_file.txt | grep -P '^(.(?!connect|CDR))*(error|:E:)(.(?!connect|CDR))*'$
Explanation of the regex:
The regular expression above is trying to match line begining(^) followed by any number(*) of any characters(.) not followed by(?!) connect nor CDR then followed by error or(|) :E: then again followed by any number of any characters not followed by connect nor CDR and then line ending($).
Your version of grep should support Perl Regex for this feature.

You can also make it case-insensitive by adding -i:  

grep -iP

Other useful hints:
  • To grep lines that contain both foo and bar you can use:
tail -f log_file.txt | grep 'foo.*bar|bar.*foo'

meaning foo comes after bar or vice versa.

  • Use grep -f FILE and it will obtain patterns from that FILE.
  • Lastly, some grep versions support --exclude-from=FILE option that lets you add exclusion patterns from a FILE:
tail -f <file> | grep --exclude-from=excludes.txt

FACEBOOK COMMENTS WILL BE SHOWN ONLY WHEN YOUR SITE IS ONLINE