Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

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

Replies are listed 'Best First'.
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

      -- '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 (Archbishop) on Jul 12, 2010 at 03:31 UTC


    #!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


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://848903]
Approved by almut
[Eily]: I do see a content, the n5
[marto]: 5 is an Odd_number
[Corion]: That could fix itself within the next 24 hours :)
[marto]: we should blame LanX
[marto]: purely because he isn't here :P
[Eily]: marto oh ok, since normally it would have been 1, which is odd as well, I didn't think this was what you were refering to
[Eily]: what? Does that mean we can't blame LanX when he's there?
[marto]: but 5 in total, is odd, where as 10 is even
[marto]: sure, we can blame LanX when he is around, but he is more likely to moan about it :P
[Eily]: there's 6 in total though :P

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (8)
As of 2018-03-20 09:56 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (249 votes). Check out past polls.