Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I use that notation at my log files:

[something-something]

somethings are necessary and variable length characters and numbers. My special log file includes line that has characters explained above. I.e.

Line 1: DEBUG [AS-kmksdf] Error occured!
Line 2: something somethingg..

I want to get Line 1. My log files grows dynamically and I show it at my screen with:

tail -f log.txt

How can I show just that special lines?

I know that I will use grep and my regex will be sth. like

[[A-Z]*-[A-Z]*]

how to combine them with that command. This doesn't work:

tail -f log.txt | grep [[A-Z]*-[A-Z]*]

I use cygwin with windows7 and can run some linux commands on my cmd.exe.

share|improve this question
    
Look at grep. – soandos Jun 20 '12 at 11:21
    
Your edit made this a completely different question, and it is still not precisely described what you actually want. No developer with experience is willing to work towards such "requirements"... – DevSolar Jun 20 '12 at 11:56
up vote 2 down vote accepted

There are a couple of problems with your regular expression:

  • First of all, I'd advice you to always use the -P switch, which enables Perl Compatible Regular Expressions.

    By default, grep uses Basic regular expression, which aren't used widely and suffer from inconsistent espacing.

  • As is [A-Z], brackets have special meaning in regular expressions. To use a literal bracket in PCREs, escape it with a backslash: \[ or \]

  • Regular expressions are case sensitive, unless you set the -i switch.

  • * means any number of occurrences, including (0). You might want to use + instead (any positive number of occurrences).

  • Always surround your regular expression with double quotes, or the bash will expand characters.

As a result, the following command will work:

tail -f log.txt | grep -Pi "\[[A-Z]+-[A-Z]+\]"
share|improve this answer
    
I have edited my question. I realized another issue for me. – kamaci Jun 20 '12 at 11:47
1  
@kamaci: Your edit requires a completely different approach, so it's an entirely different question. Instead of editing it, it's preferable to ask a new question. – Dennis Jun 20 '12 at 11:49
    
Oops my grep command has no -P option. Maybe because of cygwin. – kamaci Jun 20 '12 at 11:50
    
That's probably a bug. You could either install pcregrep or use @crizot's approach. – Dennis Jun 20 '12 at 11:59
1  
I have rollbacked my question and new question is: superuser.com/questions/439249/… – kamaci Jun 20 '12 at 12:11
tail -f log.txt |grep -E '\[[a-zA-Z]*-[a-zA-Z]*\]'

You can use Regexpal to test your regex expressions. Also you should use -E on grep. It is not mandatory, and not necessary in this case, but allow you to use extended regex expressions.

share|improve this answer
    
Recursive? Why? – DevSolar Jun 20 '12 at 11:25
    
Sory. I edit my answer, it is -E. It is not mandatory but I use -E as a rule because usualy I work with extended regex expressions. In this case it is not necessary. – criziot Jun 20 '12 at 11:30
    
that's nice. How about getting all the lines started with that special line until a line starts with number,number and : as like 14: ? – kamaci Jun 20 '12 at 11:38
    
You're right Dennis. I fixed my answer. Thanks – criziot Jun 20 '12 at 11:45
    
@Kamaci I don't think what you want is possible with just regex. You will need something like Bison. – criziot Jun 20 '12 at 11:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .