1

Sample line from a file:

one two three uname whirlcano four five

I want to extract the user names from multiple files

foreach $file (@file_names)
{
    open(my $fh, '<:encoding(UTF-8)', $file)
    or die "Could not open file '$file' $!";

    while (my $row = <$fh>)
    {
        if($row =~  "uname")
        {
            #here I want to extract only the immediate word after "uname", which is "whirlcano" in the above example.
        }
    }
}

Thanks in advance.

whirlcano
  • 43
  • 1
  • 9

1 Answers1

2

You can use a regular expression capturing group to capture the username:

while (my $row = <$fh>) {
    chomp($row); # strip newline character

    # capture first group of word characters after uname and one or more spaces
    my ($username) = $row =~ m/uname\s+(\w+)/;
    ....
}

You can change \s+ in the above example to whatever delimiter your file has, but typically when parsing CSV type files using a real CSV parser is better than regular expressions. Text::CSV_XS is one such parser that is popular.

For more information about capturing groups in regular expressions, please refer to the Capture Groups section of perldoc perlre

Hunter McMillen
  • 59,865
  • 24
  • 119
  • 170
  • Hi Hunter, these are log files with no standard delimiters. above regex is returning "uname" and not "whirlcano". This might be basic stuff, but I am not very good at regex. Please help, thanks. – whirlcano Feb 17 '16 at 17:01
  • 1
    If the row is actually the row you supplied in your question, then the regular expression I supplied will capture the username – Hunter McMillen Feb 17 '16 at 17:05