Madi has asked for the wisdom of the Perl Monks concerning the following question:

Hi friends
I need to extract the string after / character. My String has variable no of characters before /. for ex
$str=For disk partition: 0 and localhost, path is /a000/15057/37/7439
OR
$str=For disk partition: 100 and localhost, path is /a100/14959/20/4017
I need to extract the portion /a000/15057/37/7439 in the first case. ie
$path=/a000/15057/37/7439
or
/a100/14959/20/4017 in the second case.ie
$path=/a100/14959/20/4017

Thanks!!
  • Comment on have to Extract substring after '/' character

Replies are listed 'Best First'.
Re: have to Extract substring after '/' character
by davido (Cardinal) on Apr 04, 2011 at 15:31 UTC

    It's pretty easy to accomplish with Perl. The 'e' in Perl is rumored to stand for "extraction", after all. I think folks here have been hesitant to provide a cut-and-paste solution because the answer is so obvious to anyone who has spent more than a day or two learning Perl that it would probably only need to be asked by someone who is more interested in the answer than the learning. If we simply provide an answer without seeing any effort, you'll come back all too soon asking for another answer, and another, but never actually seeking wisdom. If we put more effort into providing answers than you put into learning to solve problems on your own in the future, we set ourselves up for frustration, and set you up for a lot of free solutions. We don't mind imparting knowledge, but we tire of doing people's work for free.

    One of Perl's mantra's is "There is more than one way to do it." Just about any solution including the solution to your question falls into that category. However, the Perlish way to do it is probably to use a regular expression.

    With regular expressions, you would want to match against your string $str, and capture into $1 the portion of the string you care about. You would want to test for the match, and possibly halt execution or spit out a warning if the match fails. If it succeeds, you would want to return the captured portion of the string.

    So, the steps might look like this:

    1. Create a subroutine called get_path()
    2. Inside get_path(), put $_[0] into a variable $raw_string that is lexically scoped to the sub itself using my and =.
    3. Start an if(){} block. Inside the conditional you'll be testing $raw_string against a regular expression using the =~ operator followed by the m// operator. More on that later.
    4. In the success block, your sub will return the result contained in $1.
    5. In the failure block, your sub will die while spitting out a message such as "\$raw_string failed to match in get_path().\n$raw_string\n." You will probably want to allow the script's execution to terminate here since it's an indication that the train has gone off the rails.
    6. Your script itself will invoke the sub by passing it $str and assigning the outcome to $path.

    That leaves the task of figuring out a regular expression to do the heavy lifting for you. Use capturing parenthesis. Within the capturing parens, anchor with '/', and follow that up with the token that can accept almost any input at all, in quantities of one character or more: '.+' Finally, anchor to the end of the line (or string) with a trailing '$' at the end of the pattern. Be sure to use alternate match delimiters, such as m{} because m// will run afoul of your specific first anchor (/).

    Further reading: perlre, perlretut, die, perlsub, perlsyn, and of course perlintro.


    Dave

Re: have to Extract substring after '/' character
by Corion (Patriarch) on Apr 04, 2011 at 13:59 UTC

    To help us help you better, please show the code you have already written, some representative input data, and please describe to us how the code you have fails to do what you need. Please show the output you get and the output you want, and tell us with short, simple sentences what the differences are.

Re: have to Extract substring after '/' character
by kennethk (Abbot) on Apr 04, 2011 at 14:17 UTC
      The tricks in this particular case are escaping the slash with a backslash (\/), ...

      Even better, choose a different delimiter. Instead of

      if ( $path =~ /\/path\/to\/file/ ) ...

      use

      if ( $path =~ m{/path/to/file} ) ...

      I hope this is of interest.

      Cheers,

      JohnGG

Re: have to Extract substring after '/' character
by anonymized user 468275 (Curate) on Apr 04, 2011 at 15:58 UTC
    As stated, the problem can be solved as simply as:
    ( $path ) = /(\/.*)\s*$/;

    One world, one people

Re: have to Extract substring after '/' character
by locked_user sundialsvc4 (Abbot) on Apr 04, 2011 at 17:36 UTC

    Well, the first thing that I would do is to look in http://search.cpan.org to see if someone has already published a CPAN module that deals specifically with this log-file format.   Most of the time, no matter what we find ourselves doing, “many someone elses” have already done that same thing before, and there’s a very excellent chance that they left a breadcrumbs-trail of CPAN modules behind them.

    When dealing with log-files that are formatted like this one, one of my favorite tools are the functions split and join.   For instance, if we split any of the strings in your example, we get a result such as ('', 'a000', '15057', '37', '7439').   Now, we can manipulate the elements individually, then reconstruct the string from the array using join.

    The first incarnation of Perl actually came to be, because of the (ever-present) need to do tasks exactly like yours.

A reply falls below the community's threshold of quality. You may see it by logging in.