PDA

View Full Version : Vi reg exp find string



fokuslee
May 3rd, 2012, 03:13 PM
Hi All
I have a question on building a regular expression, this is probably the best place for it
I only have Vi on an Unix machine and need to look at an error log.
I know the time of the error was
May<white spaces>2<white spaces>06:<some minutes>:<some seconds>

I was in Vi trying to find this string but my expression is not correct at all
what is the reg exp equivalent of my search string?
Also how do you find next and find previous in Vi

Thanks abunch

SuaSwe
May 3rd, 2012, 03:37 PM
Hi fokuslee,

To find next and previous, use / to search then just press n for "next" and p for "previous". :)

You could use cat instead of vi to display to terminal and use "May 2 06:" for example, inserting or removing whitespace as necessary. Far as I know you can use:



/May 2 06:


(for example) in escape mode within vi as well, to search for instances of that string. Once it's found it, use n and p to toggle between the results. Does this help or have I misunderstood the question?

fokuslee
May 3rd, 2012, 04:36 PM
/May 2 06: is the first thing i tried but returned no pattern found
so i suspect it's to do with white spaces. Maybe there is 3 instead of 2 white spaces? what is the regular expression of one or more white spaces? [].* ?

Lars Noodén
May 3rd, 2012, 05:49 PM
A regular expression with zero or more whitespaces can be done like this: /May[[:space:]]*2

Also when working with logs you might consider using less (http://manpages.ubuntu.com/manpages/precise/en/man1/less.1.html) or egrep (http://manpages.ubuntu.com/manpages/precise/en/man1/egrep.1.html). You can also pipe grep through less:
egrep "May[[:space:]]*2" /var/log/some.log | less

fokuslee
May 3rd, 2012, 06:19 PM
Thanks all
problem solved

codemaniac
May 3rd, 2012, 06:49 PM
A regular expression with zero or more whitespaces can be done like this: /May[[:space:]]*2

Also when working with logs you might consider using less (http://manpages.ubuntu.com/manpages/precise/en/man1/less.1.html) or egrep (http://manpages.ubuntu.com/manpages/precise/en/man1/egrep.1.html). You can also pipe grep through less:
egrep "May[[:space:]]*2" /var/log/some.log | less
Lars Noodén ,
your regex will match zero or more occurences of white spaces .
The records that dont have spaces between May and 2 will also get matched by your regex .
Consider the below input .


May 2 06:
May206:
May 2 06:
The egep regex will match the 2nd row also , but the OP dont wnat that .The below would ensure one or more spaces between month and date .

"May[[:space:]]+2"

Lars Noodén
May 3rd, 2012, 06:57 PM
@codemaniac: thanks for catching that. I have the bad habit of using * everywhere, even when it's not correct. + is the right answer.