TonyNY has asked for the wisdom of the Perl Monks concerning the following question:

Hi. I'm new to perl so please excuse my newbie-ness :) Is there a way to print just a word contained in a text file by using a search filter instead of printing the entire string even if the word is not an exact match? I have a text file that contains some server names with the fqdn and some with the short names that are contained in a string. example: <Answer type="string">ServerName.FD.net.org</Answer> The following code finds the string and prints it but I would like to just print the ServerName:
my $file = "computernames.txt"; open FILE, "$file"; while ($line=<FILE>){ if ($line=~Servername){ print $line; } }
Regards, Tony

Replies are listed 'Best First'.
Re: Print word from text file that is not an exact match
by NetWallah (Canon) on Jun 09, 2018 at 04:07 UTC
    You can use proper regex syntax to "capture" what you want :
    if (my ($name) = $line=~/(Servername[\.\w]*)/){ print "$name\n"; }

                    Memory fault   --   brain fried

      Hi NetWallah, Thanks so much for your quick response and a solution that works! If you can please take a look at the following code and let me know what I am doing wrong I would greatly appreciate it. If I can get this to work then I will be able to accomplish what I need to do for my first perl script. I think I am close to getting this to worK? Thanks,
      my $file = "computers.txt"; print "-->>Paste computer name(s) here, press enter key, then ctrl-D k +eys<<--\n"; @computers = (<STDIN>); foreach (@computers) { open FILE, "$file"; while ($line=<FILE>){ if (my ($name) = $line=~/(@computers[\.\w]*)/){ print "name\n"; else print "no match found\n"; close FILE, "$file"; } } }
      Regards, Tony
        Paste computer name(s) here, press enter key,

        Where are you copying the names from, is is another text file ? If so, please show example

        poj
Re: Print word from text file that is not an exact match
by Veltro (Hermit) on Jun 09, 2018 at 09:18 UTC
      Hi Veltro, Thanks so much for the links. I will definitely check them out while I continue to learn from my O'Reilly Learning Perl book. Regards, Tony
Re: Print word from text file that is not an exact match
by Laurent_R (Canon) on Jun 09, 2018 at 16:07 UTC
    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.

      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.