Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re^5: Print word from text file that is not an exact match

by poj (Abbot)
on Jun 09, 2018 at 18:58 UTC ( [id://1216272]=note: print w/replies, xml ) Need Help??


in reply to Re^4: Print word from text file that is not an exact match
in thread Print word from text file that is not an exact match

There are ways to avoid repeat searches of each line but this should get you started.

#!/usr/bin/perl use strict; my $count = 0; my $namefile = $ARGV[0] || 'names.txt'; # default if no argument # names.txt #ServerName1 #ServerName2 # get list of computers to search for my @computers; open FILE,'<',$namefile or die "Could not open $namefile : $!"; while (<FILE>){ chomp; s/^\s+|\s+$//g; # trim spaces next unless /\S/; # skip blank lines push @computers,$_; } close FILE; my $file = "computernames.txt"; # computernames.txt #<Answer type="string">ServerName1.FD.net.org</Answer> #<Answer type="string">ServerName2.FD.net.org</Answer> #<Answer type="string">ServerName3.FD.net.org</Answer> # search text file open IN, '<',$file or die "Could not open $file : $!"; while (my $line = <IN>){ # repeat line search for each computer foreach my $search (@computers) { if ( my ($name) = $line =~ /($search[\.\w]*)/ ){ print "Name ; $name\n"; ++$count; } } } close IN; # result if ($count){ print "$count matches\n"; } else { print "No matches found\n"; }
poj

Replies are listed 'Best First'.
Re^6: Print word from text file that is not an exact match
by TonyNY (Beadle) on Jun 09, 2018 at 21:45 UTC
    Perfect, exactly what I was looking for. I need the input from the user of the script so all I needed to do was to redirect the computer names from the array to the names.txt file and it worked like a charm. BAM! Thanks so much for your help poj -Tony
      Hi poj... Your solution works great but is there a way to modify it to match the entire computer name if a part of the computer name is omitted? When a part of the computer name is omitted it returns only all of the posibilities of the ommitted variation of the name instead of all the possibilites of the complete computer name. For example: if the C is omitted from Computer1 the search will return omputer1.domain.org, omputer2.domain.org, omputer3.domain.org, etc... or if the Comp part is omitted the result would be uter1.domain.org, uter2.domain.org, uter3.domain.org, etc... If the middle to the end of the name is omitted that works out just fine but if the middle or just the end of the computer name is searched for it will only return that part in the search results. I tried to modify the regex line but I have just been spinning my wheels. Many Thanks, Tony

        Try modifying the regex to

        if ( my ($name) = $line =~ />(.*$search[^<]*)/ ){

        poj

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2024-04-24 07:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found