LendKey

Thursday, April 23, 2009

How to match lines that does not start with certain string(s)

I had a task to parse a huge log file and needed to pick up only those lines started with certain strings like:
CPU_ALL
ZZZZ
etc.

So I fired up Eclipse, opened the file, and opened Find/Replace dialog box. In the dialog, I tried to replace:
^\b(?!CPU_ALL)(?!ZZZZ)\w+\b.*$

with
Blank

So Eclipse find all strings that does not start with CPU_ALL nor ZZZZ and replaced those lines with space.

I was able to do another Replace of ^\n$ with Blank, so all blank lines got removed. But some how Eclipse complaint I had incompatible end of lines. So I opened the text file with my favorite text editor Notepad++ and ran its "Remove blank lines" plugin to get the job done.

The key here is
(?!CPU_ALL)

which matches string that does not start with "CPU_ALL".
And another
(?!ZZZZ)
right after allows me to exclude another string.

Cheers!