Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Question on File position, pulling words

by taints169 (Initiate)
on Mar 04, 2008 at 03:54 UTC ( [id://671782]=perlquestion: print w/replies, xml ) Need Help??

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

Hey wize ones. Ok, got a pretty small file, it's actually just one long line. I need to pull the last two variables out of the line, problem is the length of the line changes from time to time, so I dont know the exact position... Here's what the file looks like:
2XHENVVAR IBM_JAVA_COMMAND_LINE=/opt/was5/base/crm/java/bin/java +-Djava.net.preferIPv4Stack=true -Dwas.status.socket=55962 -Xbootclass +path/p:/opt/was5/b ase/crm/java/jre/lib/ext/ibmorb.jar:/opt/was5/base/crm/java/jre/lib/ex +t/ibmext.jar -classpath /prod/wesadm/wes/was5/base/instances/crm-unit +c-01_01/properties :/opt/was5/base/crm/properties:/opt/was5/base/crm/lib/bootstrap.jar:/o +pt/was5/base/crm/lib/j2ee.jar:/opt/was5/base/crm/lib/lmproxy.jar:/opt +/was5/base/crm/lib /urlprotocols.jar -Xms50m -Xmx256m -Dws.ext.dirs=/opt/was5/base/crm/ja +va/lib:/prod/wesadm/wes/was5/base/instances/crm-unitc-01_01/classes:/ +opt/was5/base/crm/ classes:/opt/was5/base/crm/lib:/opt/was5/base/crm/lib/ext:/opt/was5/ba +se/crm/web/help:/opt/was5/base/crm/deploytool/itp/plugins/com.ibm.eto +ols.ejbdeploy/runt ime -Dserver.root=/prod/wesadm/wes/was5/base/instances/crm-unitc-01_01 + -Ddb2j.system.home=/prod/wesadm/wes/was5/base/instances/crm-unitc-01 +_01/cloudscape -Du ser.install.root=/prod/wesadm/wes/was5/base/instances/crm-unitc-01_01 +-Dcom.ibm.itp.location=/prod/wesadm/wes/was5/base/instances/crm-unitc +-01_01/bin -Dwas.i nstall.root=/opt/was5/base/crm -Dibm.websphere.preload.classes=true -D +java.io.tmpdir=/var/adm/corefiles/wes/crm_dceweb_ear-unitc -DIBM_HEAP +DUMP=true -DIBM_HE AP_DUMP=true -DIBM_HEAPDUMPDIR=/var/adm/corefiles/wes/crm_dceweb_ear-u +nitc -DIBM_HEAPDUMP_OUTOFMEMORY=true -DIBM_JAVADUMP_OUTOFMEMORY=true +-DIBM_JAVA_HEAPDUM P_TEXT=true -Djava.security.auth.login.config=/prod/wesadm/wes/was5/ba +se/instances/crm-unitc-01_01/properties/wsjaas.conf -Djava.security.p +olicy=/prod/wesadm /wes/was5/base/instances/crm-unitc-01_01/properties/server.policy com. +ibm.ws.bootstrap.WSLauncher com.ibm.ws.runtime.WsServer /prod/wesadm/ +wes/was5/base/inst ances/crm-unitc-01_01/config crm-unitc-01 host1_crm-unitc-01_01 id_ser +ver_ear-unitc
I need the last 2 variables...
host1_crm-unitc-01_01 id_server_ear-unitc
Any ideas?

Replies are listed 'Best First'.
Re: Question on File position, pulling words
by Narveson (Chaplain) on Mar 04, 2008 at 04:19 UTC

    Are your variables delineated by whitespace?

    To get the last two whitespace-delineated terms out of $_, say

    my @last_two = /.*\s(\S+)\s+(\S+)/;

      Almost correct, you need to anchor the regex at the end of the string.

      my @last_two = $string =~ /(\S+)\s+(\S+)$/;

        Don't overlook my .*. As you can learn in perlretut,

        the first quantifier .* grabs as much of the string as possible while still having the regexp match.

        My submission said

        /.* # Read as far as possible before \s # matching whitespace, (\S+) # capturing a word, \s+ # matching more whitespace, (\S+) # and finally capturing the last word. /x
Re: Question on File position, pulling words
by ysth (Canon) on Mar 04, 2008 at 06:41 UTC
    To get the last two whitespace-delimited fields, you could use split, like:
    my @line_fields = split " ", $line; my $penultimate = $line_fields[-2]; my $last = $line_fields[-1];
    or more succinctly:
    my ($penultimate, $last) = (split " ", $line)[-2, -1];

      Or

      my ($last, $penultimate) = reverse split " ", $line;

      or even

      my ($last, $penultimate) = reverse split for $line;
      This actually breaks them into seperate variables. Thank you!!!!!
Re: Question on File position, pulling words
by Punitha (Priest) on Mar 04, 2008 at 04:49 UTC

    Consider your variables are separated by space, then you can try like,

    use strict; while(<DATA>){ chomp; print "IN:$&\n" if($_ =~/\s[^\s]+\s[^\s]+$/); }

    Punitha

      Perfect thanks everyone! If I wanted to break up the line into 2 seperate variables, would I just search on the space then replace with a new line? I'd like to be able to assign a variable to each one and call each one.
      host_crm-unitc-01_01 server_one_ear-unitc to get: host_crm-unitc-01_01 server_one_ear-unitc

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://671782]
Approved by planetscape
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (6)
As of 2024-04-19 11:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found