在perl中删除文件中的特定行

时间:2016-01-25 08:17:57

标签: perl

我想删除HPOM的行,并只保留行节点:

我的代码:

#!/usr/bin/perl
use strict;

open (FILE, "/tmp/agentstas.dat");

my @lines = <FILE>;

my $lines;

my $string = "Node";

my $badstring = "hpom";

foreach $lines (@lines) {

    if ($lines =~ $string || $lines !=~ $badstring)  { print "$lines"; }

}

结果:

Node atum1:
HPOM Managed Node status :
Node vmatum3:
HPOM Managed Node status :
Node ls_sfda-new:
HPOM Managed Node status :
Node risto4:
HPOM Managed Node status :
Node jzn18z1:
HPOM Managed Node status :
Node sqlcatom:
HPOM Managed Node status :
Node new-sspc-as:
HPOM Managed Node status :
Node dafna:
HPOM Managed Node status :
Node adonis:
HPOM Managed Node status :
Node hydra:
HPOM Managed Node status :
Node kukuad:
HPOM Managed Node status :
Node janus1:
HPOM Managed Node status :
Node janus2:
HPOM Managed Node status :
Node zubin.extranet.gov.il:
HPOM Managed Node status :
Node nyx-as:
HPOM Managed Node status :
Node keto1:
HPOM Managed Node status :
Node sql12ao1:
HPOM Managed Node status :
Node keto2:
HPOM Managed Node status :
Node durge:
HPOM Managed Node status :
Node vcmanage:
HPOM Managed Node status :
Node vcmobile:
Node vishnu:
HPOM Managed Node status :
Node ram:
HPOM Managed Node status :
Node brizo.iaadom:
HPOM Managed Node status :
Node krishna:
HPOM Managed Node status :
Node mars1:
HPOM Managed Node status :
Node mars2:
HPOM Managed Node status :
Node rasas:
HPOM Managed Node status :
Node hypons.iaadom:
HPOM Managed Node status :
Node nnm-as.iaadom:
HPOM Managed Node status :
Node risto3:
HPOM Managed Node status :
Node ivrmanage.iaadom:
HPOM Managed Node status :
Node mpsair01:
HPOM Managed Node status :
Node mpsland01:
HPOM Managed Node status :
Node atum2.iaadom:
HPOM Managed Node status :
Node vmatum4.iaadom:
HPOM Managed Node status :
Node rastas:
HPOM Managed Node status :
Node nnm-ls.iaadom:
HPOM Managed Node status :
Node rasls:
HPOM Managed Node status :
Node ask1:
HPOM Managed Node status :
Node ask2:
HPOM Managed Node status :
Node grid2:
HPOM Managed Node status :
Node grid1:
HPOM Managed Node status :
Node ivrdb:
HPOM Managed Node status :
Node dafn:
HPOM Managed Node status :
Node mist1:
HPOM Managed Node status :
Node mist2:
HPOM Managed Node status :
Node amun:
HPOM Managed Node status :
Node amun1:
HPOM Managed Node status :
Node dagon:
HPOM Managed Node status :
Node dagon1:
Node davlin2:
HPOM Managed Node status :
Node cet:
HPOM Managed Node status :
Node sdns:
HPOM Managed Node status :
Node pdns:
HPOM Managed Node status :
Node hermes1.iaadom:
HPOM Managed Node status :
Node tyr2:
HPOM Managed Node status :
Node tyr1:
HPOM Managed Node status :
Node new_sspcls:
Node hapy1:
Node geb:
Node saga:
Node indra:
Node yama:

4 个答案:

答案 0 :(得分:3)

对文件名进行硬编码通常是一个坏主意。如果使用Unix过滤器模型,则程序更灵活(从STDIN读取,写入STDOUT)。

#!/usr/bin/perl

while (<>) {
  print unless /^HPOM/;
}

将其运行为:

$ ./your_filter.pl < /tmp/agentstas.dat > output_file

答案 1 :(得分:2)

始终使用三个参数和词法文件句柄来执行文件操作 如果您要删除所有行,请从&#39; HPOM&#39;开始,您可以将代码编写为:

open my $fh, "<", "/tmp/agentstas.dat" or die $!;

my @lines = <$fh>;

foreach my $lines (@lines)
{
    next if ($lines =~ m/^HPOM/i);
    print "$lines";
}

或者更好地使用while loop

while (my $lines = <$fh>)
{
    chomp $lines;
    next if ($lines =~ m/^HPOM/i);
    print "$lines\n";
}

或者在您的代码中更改此行:

if ($lines =~ $string || $lines !=~ $badstring)  { print "$lines"; }

if ($lines =~ /^$string/i || $lines !~ /^$badstring/i)  { print "$lines"; }

^ =匹配字符串的开头。
i =不区分大小写的模式匹配。

i因为你用小写字母和输入文件取$badstring它是大写字母。

答案 2 :(得分:2)

您的初始代码太复杂了。试试这个:

#!/usr/bin/env perl

use strict;
use warnings; 

open (my $input, '<', "/tmp/agentstas.dat") or die $!;

while ( <$input> ) {
   print if m/^Node/i;
}
close ( $input );

答案 3 :(得分:1)

你走在正确的轨道上。该错误处于if状态。

if ($lines =~ $string || $lines !=~ $badstring)

应该是

if ($lines =~ $string && $lines !=~ $badstring)

因为你希望两个条件都为真(AND,而不是OR)。

使用perl的-n切换,我们可以将其解决为一个很好的单行:

$ perl -ne '/Node/ && ! /hpom/ && print' /tmp/agentstas.dat

你甚至不必使用perl,你可以使用

$ grep -e ^Node /tmp/agentstas.dat

仅列出以&#34开头的行;节点&#34; (这似乎是你想要实现的目标)。