Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
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 ( [id://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;

Replies are listed 'Best First'.
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 ww (Archbishop) on Mar 26, 2011 at 20:10 UTC
Re: Scanning a file for a match, printing different part of the line when found.
by jwkrahn (Abbot) 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 (Curate) 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 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?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
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?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-03-29 08:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found