0

I'm stuck trying to figure out the perl regex to extract the following values: (the /home/mail/dump dirs will always be the same). Thanks.

abc

123456

This is the output:

drwxr-xr-x   - mail_hd mail_users          0 2011-03-26 20:12 /home/mail/dump/abc
drwxr-xr-x   - mail_hd mail_users          0 2011-04-15 09:10 /home/mail/dump/123456

4 Answers 4

2

Maybe use split and basename, instead.

Sign up to request clarification or add additional context in comments.

1 Comment

use File::Basename qw( basename ); basename((split(' ', $_, 8))[-1])
1

This is similar to this.

Split on "/" and get the last item

awk -F"/" '{print $NF}' file

Ruby(1.9+) (similar for Perl)

$ ruby -ne 'print $_.sub(/.*\//,"")' file
abc
123456

$ ruby -F"/" -ane 'print $F[-1]' file
abc
123456

3 Comments

Since the OP asked for Perl, perl -F/ -lane'print $F[-1]' file
@ikegami. you should supply that as an answer.
thanks guys, these are all good answers. its cool to see the other versions. i'll go with these.
1

The following perl regex will give just the characters after the final forward slash.

s=.*/==

Example:

$ echo 'wxr-xr-x   - mail_hd mail_users          0 2011-03-16 18:46 /home/mail/dump/abc' | perl -pe 's=.*/=='
abc

Comments

0

Maybe this is usefull, should return what you need.

if ($subject =~ m!/home/mail/dump/(.*)$!) {
    $result = $1;
} else {
    $result = "";
}

it matches everything after /home/mail/dump/ until end of line

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.