Show matched lines for a given regex at Linux

-1

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.

kamaci

Posted 2012-06-20T11:18:26.887

Reputation: 111

Look at grep. – soandos – 2012-06-20T11:21:40.597

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 – 2012-06-20T11:56:12.563

Answers

2

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]+\]"

Dennis

Posted 2012-06-20T11:18:26.887

Reputation: 42 934

I have edited my question. I realized another issue for me. – kamaci – 2012-06-20T11:47:04.150

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 – 2012-06-20T11:49:23.357

Oops my grep command has no -P option. Maybe because of cygwin. – kamaci – 2012-06-20T11:50:31.667

That's probably a bug. You could either install pcregrep or use @crizot's approach. – Dennis – 2012-06-20T11:59:38.287

1

I have rollbacked my question and new question is: http://superuser.com/questions/439249/show-starting-with-a-special-line-until-a-special-line-regex-for-linux-tail-comm

– kamaci – 2012-06-20T12:11:22.357

1

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.

fmanco

Posted 2012-06-20T11:18:26.887

Reputation: 2 287

Recursive? Why? – DevSolar – 2012-06-20T11:25:59.607

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. – fmanco – 2012-06-20T11:30:15.560

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 – 2012-06-20T11:38:22.570

You're right Dennis. I fixed my answer. Thanks – fmanco – 2012-06-20T11:45:19.567

@Kamaci I don't think what you want is possible with just regex. You will need something like Bison.

– fmanco – 2012-06-20T11:47:50.577