Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Extracting text from a line

by kirkbrown (Novice)
on Jul 12, 2010 at 00:41 UTC ( #848903=perlquestion: print w/ replies, xml ) Need Help??
kirkbrown has asked for the wisdom of the Perl Monks concerning the following question:

Given a string of text like: UID: dlsmith3

How would I extract "dlsmith3" and print it to a separate file?

I want to avoid extracting strings from strings like Supervisor UID: plbaerr4

Comment on Extracting text from a line
Re: Extracting text from a line
by almut (Canon) on Jul 12, 2010 at 00:49 UTC
    my ($uid) = $string =~ /^UID: (\w+)/; print $fh $uid if $uid;

    (where $fh is a file handle opened for writing to the other file)

      print $fh $uid if $uid;

      you meant to say
      print $fh $1 if $uid;
      I guess.

      update almut was right
      I thought it was scalar context, missed to notice that "(" ")" => "list context" ,my mistake :-).
      Thanks to almut and pemungkah



      Vivek
      -- 'I' am not the body, 'I' am the 'soul/consciousness', which has no beginning or no end, no attachment or no aversion, nothing to attain or lose.

        What's the difference, semantically?

        Note that in list context (my ($uid) = ...), the match operation returns the captures, so $uid holds the same value as $1.  If there is no match, both values are undef.

Re: Extracting text from a line
by ww (Bishop) on Jul 12, 2010 at 03:31 UTC

    Or

    #!usr/bin/perl use strict; use warnings; # 848903 my @strings = ("Supervisor UID: plbaerr4", "UID: dlsmith3", "Peon UID: jklmn2"); for my $string(@strings) { if ( $string !~ /^Supervisor \w+/ && $string =~ /UID: (\w+)/ ) { print "$1 \n"; } else { print "No match in $string \n"; } }

    almut's reply handles the file-writing spec; this addresses ambiguity in OP's spec: is it the "Supervisor" UID that's verbotten or any UID preceded by a title?

    Updated: preceding para dropped from cut and thus not pasted. Duh.

Re: Extracting text from a line
by nvivek (Vicar) on Jul 12, 2010 at 04:23 UTC

    You try this.It is similar to almut's answer but we don't know whether there will be space between UID: and string.Following will solve that problem and it works.

    #!usr/bin/perl use strict; use warnings; my @input = ("Supervisor UID: plbaerr4", "UID:dlsmith3", "Peon UID: jklmn2"); open FH,">output.txt" or die "can't open file for writing:$!"; for my $string (@input) { print FH $1 if($string=~/^UID:\s*(\w+)/); } close FH;
Re: Extracting text from a line
by suhailck (Friar) on Jul 12, 2010 at 05:09 UTC
    In Unix/Linux,

    perl -ne 'print $1 if /^UID:\s*(\w+)/' fileRead > fileWrite


    ~suhail

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2014-08-22 05:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (147 votes), past polls