尝试使用RegEx在文件中查找特定字符串

时间:2018-06-13 08:59:57

标签: regex string perl file stdin

我有一个格式如下的文件:

define host{
     use             generic-printer
     host_name       imp-p-125
     address         100.68.22.10
     hostgroups      network-printers
    }

 define service{
        use     generic-service
        host_name       imp-p-125
        service_description     Toner 1 Status
        check_command   check_toner1
        check_interval  240
        retry_interval  2
        notification_interval   245
        }

我正在尝试找到host_name行(1imp-p-1251),目的是不重复文件中存在的主机。

我有以下代码来执行此操作,但它总是告诉我“找到”我放入键盘的所有名称。

sub openFile {

  open(FILE, "/home/server/test2.txt");
  print "file open!\n";
  print "hostname(Example 'imp-p-125'): ";

  my $name = <STDIN>;
  chomp $name;

if (grep{$name} <FILE>){
      print "found\n";
}else{
    print "word not found\n";
}
  close FILE;
}

我正在搜索使用RegEx和STDIN方法的选项,但我找不到任何东西。

提前致谢。

1 个答案:

答案 0 :(得分:0)

你误解了grep函数的作用。它为传递给它的每个元素计算表达式(在本例中为$name),如果为true,则返回该元素。如果$name包含一个值,那么它将始终为true,因此它将返回文件中的每一行,并且它将始终打印“Found”结果。

相反,您想要使用正则表达式。这就是正则表达式的样子。

if($somevalue =~ /pattern/)

您希望处理每一行,因此您还需要一个循环,例如while循环。如果省略$somevalue,就像许多Perl函数和运算符一样,它将默认为$_,这个循环将用于为您提供文件的每一行。由于$name可能包含在正则表达式中被认为是特殊的字符,因此使用\ Q和\ E将其包围起来意味着它将被视为常规字符。

my $found=0;
while(<FILE>)
  {
  if( /\Q$name\E/ )
    {
    $found=1;
    }
  }
if($found)
  {
  print "Found\n";
  }
else
  {
  print "word not found\n";
  }

您还使用了过时的打开文件的方法,也没有检查它是否已打开。考虑用这个替换它

if(open(my $file, "<", "/home/server/test2.txt"))
  {
  # Your code to process the file goes inside here
  close($file);
  }

PS不要忘记将<FILE>替换为<$file>