Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Scanning a file for a match, printing different part of the line when found.

by cheech (Beadle)
on Mar 26, 2011 at 18:15 UTC ( #895678=perlquestion: print w/ replies, xml ) Need Help??
cheech has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I found this code to scan a file and print each occurrence of a string. As it is, the exact search string is printed. How can I print a different part of the line when the string is found? Example: Search for "Name:", and every time it is found, print just the name itself (which falls one space after the colon) and not the search string "Name:".
#!/usr/bin/perl -w use strict; use IO::File; use constant FILE => 'search.txt'; use constant FIND => 'string to find'; IO::File->input_record_separator(FIND); my $fh = IO::File->new(FILE, O_RDONLY) or die 'Could not open file ', FILE, ": $!"; $fh->getline; #fast forward to the first match #print each occurence in the file print IO::File->input_record_separator while $fh->getline; $fh->close;

Comment on Scanning a file for a match, printing different part of the line when found.
Download Code
Re: Scanning a file for a match, printing different part of the line when found.
by ELISHEVA (Prior) on Mar 26, 2011 at 18:55 UTC

    How can I print a different part of the line when the string is found?

    What have you tried?

Re: Scanning a file for a match, printing different part of the line when found.
by wind (Priest) on Mar 26, 2011 at 19:20 UTC
    Maybe it's time to start learning instead of just "finding code"?
    use strict; use warnings; my $file = 'search.txt'; open my $fh, $file or die $!; while (<$fh>) { print "$1\n" if /Name:\s*(.*)/; }
      Thanks wind
Re: Scanning a file for a match, printing different part of the line when found.
by jwkrahn (Monsignor) on Mar 26, 2011 at 19:27 UTC
    I found this code to scan a file and print each occurrence of a string.

    That only works correctly if the "search string" is not at the end of the file, otherwise it prints one less than the correct number.

    And even though you are using the "getline" method you are not actually reading "lines" because you are re-defining the Input Record Separator from its normal value of "\n" to something else so you are actually reading "records".

Re: Scanning a file for a match, printing different part of the line when found.
by philipbailey (Chaplain) on Mar 26, 2011 at 19:42 UTC

    You can do this sort of thing with a one-liner. You don't specify what you want to match in much detail, but here is an example:

    perl -ne 'print "$1\n" if /Name:\s*(.+?)\b/' search.txt
      Since OP wasn't specific about how "Name" is constructed, ++

      But note that this will not satisfy the op by printing the (whole) name field, if "Name" is comprised of more than one word (for example: Firstname MI Lastname), because of the \b

Re: Scanning a file for a match, printing different part of the line when found.
by ww (Bishop) on Mar 26, 2011 at 20:10 UTC

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2014-08-30 19:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (293 votes), past polls