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

I'm trying to extract a specific data from a
one line file that contains the below information:
## /loc1/loc2/loc3/848xxxxxB01_d_1
while I was able to do it with the code below,
how do I get the same information if the line
had looked like:
## /loc1/loc2/loc3/loc4/848xxxxxB01_d_1
or
## /loc1/848xxxxxB01_d_1
OUTPUT would look like this: 848xxxxxB01_d_bot

Thanks... RCP
#! perl -w open(MYOUTFILE, ">/tmp/design_name_bot"); open (FILEH, "/tmp/strfile") +;while (<FILEH>) { chomp; if ( /848*/i ) { tr/\// /; tr/_/ /; ($board,$iss) = (split)[4,5]; ($line) = "_"; ($side) = "bot"; open(MYOUTFILE, ">>/tmp/design_name_bot"); print MYOUTFILE "$board$line$iss$line$side\n"; } } close(MYOUTFILE);

Replies are listed 'Best First'.
Re: extracting data from a line
by duff (Parson) on Oct 22, 2004 at 15:46 UTC

    One way:

    s[.*/][]; my($board,$iss,$side) = split /_/;

    that removes everything up to and including the last / character which would leave only the part you're interested in.

    Since these look like filenames you might want to use File::Basename instead.

Re: extracting data from a line
by gothic_mallard (Pilgrim) on Oct 22, 2004 at 15:55 UTC

    Not totally sure what you're wanting from this... but if I'm reading correctly then this should work:

    while (<DATA>) { /\/([^\/]+)\d$/; print $1."bot\n"; } __DATA__ ## /loc1/loc2/loc3/848xxxxxB01_d_1 ## /loc1/loc2/loc3/loc4/848xxxxxB01_d_1 ## /loc1/848xxxxxB01_d_1

    Obviously here I'm replacing your input file with the __DATA__ block for ease of example and naturally you could change the print $1."bot\n"; to print MYOUTFILE $1."bot\n";.

    --- Jay

    All code is untested unless otherwise stated.

      Thanks for your input Jay. Your code worked well with what
      I wanted to do. But...now came another glinch.
      What if the input file looked like this:
      c:/loc1/loc2/loc3/84xxxxxB01_e1_1

      When ran, the program output looked like this:
      84xxxxxB01e_bot
      when it should look like this
      84xxxxxB01_e1_bot

      Do I need to remove the first two characters first
      before running your "line" (/\/(^\/+)\d$/;) or
      is there a way to this with your one "line" of code?

      Bare with me as I don't get into programming with perl
      as much as I'd like, but I'm trying.

      Thanks..RCP

        I just ran that data line ( c:/loc1/loc2/loc3/84xxxxxB01_e1_1) through the above code and got 84xxxxxB01_e1_bot as expected, so I'm not sure why it didn't work for you.

        while (<DATA>) { /\/([^\/]+)\d$/; print $1."bot\n"; } __DATA__ c:/loc1/loc2/loc3/84xxxxxB01_e1_1

        --- Jay

        All code is untested unless otherwise stated.
        All opinions expressed are my own and are intended as guidance, not gospel; please treat what I say as such and as Abigail said Think for yourself.
        If in doubt ask.

Re: extracting data from a line
by Random_Walk (Prior) on Oct 22, 2004 at 16:11 UTC
    perl -ne'@a=split /\//; print pop @a'

    Cheers,
    R.

      #! perl -w open(MYOUTFILE, ">/tmp/design_name_bot"); open (FILEH, "/tmp/strfile"); $_ = <FILEH>; print MYOUTFILE substr($_, rindex($_, '/') +1); close(FILEH); close(MYOUTFILE);
      Substr is the most efficient method, and he specified a one line file.

        TedPride++ I had not thought of the substr($_, rindex($_, '/') +1) way to get the last 'element' off a line and this is something I find I am coding a lot here. Its always nice to learn a more efficient way to do things.

        Thanks,
        R.