Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

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

by Laurent_R (Canon)
on Jun 09, 2018 at 16:07 UTC ( [id://1216250]=note: print w/replies, xml ) Need Help??


in reply to Print word from text file that is not an exact match

Regex capture is certainly the first option that comes to mind.

However, depending on how your data file is structured (BTW, it would be very useful to provide a data sample), you might also want to have a look at the split function.

  • Comment on Re: Print word from text file that is not an exact match

Replies are listed 'Best First'.
Re^2: Print word from text file that is not an exact match
by TonyNY (Beadle) on Jun 09, 2018 at 18:34 UTC
    Hi Laurent, The text file is a dump of a query and looks something like this:
    <Answer type="string">ServerName1.org</Answer> <Answer type="string">ServerName2.org</Answer> <Answer type="string">ServerName3.org</Answer>
    Regards, Tony
      A regex capture, as shown by NetWallah and poj, is the easiest solution with your data.

      Let me, however, continue with my earlier idea and show how split could be used in such a case. The following is a demonstration under the Perl debugger:

      DB<1> $string = '<Answer type="string">ServerName.FD.net.org</Answer +>'; DB<2> @fields = split /[<>]/, $string; DB<3> x @fields; # displaying the content of the @fields array +after the split 0 '' 1 'Answer type="string"' 2 'ServerName.FD.net.org' 3 '/Answer' DB<4> print $fields[2]; # outputting the server name ServerName.FD.net.org
      You could also retrieve directly the server name without using a temporary array:
      DB<1> $string = '<Answer type="string">ServerName.FD.net.org</Answer +>'; DB<2> $name = (split /[<>]/, $string)[2]; DB<3> print $name; ServerName.FD.net.org
      But, again, a regex capture is simpler with your data format.
        Hi Laurent, I think I understand what you are explaining to me but would not know how to incorporate your example into the following code:
        #!/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"; }
        Thanks, Tony

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-04-25 06:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found