rblog

Using Notepad++ and regex to search and append text

Needed to search an access.log file from Apache where each line started with an ip-address. Due to a limitation in AWstats, see bityard.org/…/apache_awstats_x-forwarded-for I did have to update the current log file (and later update httpd.conf to get rid of difference in # of fields)

Anyhow, in order to get a workaround I needed to add the missing hyphen ‘-‘ for all requests that does not use the load balancer. Pretty simple, many blog posts on how to do it, e.g stackoverflow.com/…/using-regex-to-prefix-and-append-in-notepad. To append, use \1 (and \2 etc if needed). My regex expression was

^(?:[0-9]{1,3}\.){3}[0-9]{1,3}\s-

Confirmed by using http://gskinner.com/RegExr/.
Problem was that the ‘\1’ did not work…until I RTFM. I needed to add a parenthesis to create a collection so that Notepad++ (Or more correct the Regex-engine knew what I refer to when using ‘\n’).

(^(?:[0-9]{1,3}\.){3}[0-9]{1,3}\s-)