I’m still learning regex, and have a long ways to go so would appreciate help from any of you with more regex experience. I’m working on a perl script to parse multiple log files, and parse for certain values. In this case, I’m trying to get a list of user names.
Here’s what my log file looks like:
[date timestamp]UserName = Joe_Smith
[date timestamp]IP Address = 10.10.10.10
..
Just testing, I’ve been able to pull it out using \UserName\s\=\s\w+, however I just want the actual UserName value, and not include the ‘UserName =’ part. Ideally if I can get this to work, I should be able to apply the same logic for pulling out the IP Address etc, but just hoping to get list of Usernames for the moment.
Also, the usernames are always in the format above of Firstname_Lastname, so I believe \w+ should always get everything I need.
Appreciate any help!
You should capture the part of the matched string that you are interested in using parentheses in the regular expression.
If the match succeeds, then captures are available in the built-in variables
$1,$2etc, numbered in the order that their opening parenthesis appears in the regular expressions.In this case you need only a single capture so you need look only at
$1.Beware that you should always check that a regex match succeeded before using the values in the capture variables, as they retain the values from the last successful match and a failed match doesn’t reset them.
output