删除具有多种搜索模式的行 perl
remove lines perl with multiple search patterns
我正在尝试找到一种方法来使用 perl 删除包含以下内容的行:
errors:
mirror
raid*
pond
我找到了:
perl -pi -e "s,errors:,,"
有没有一种方法可以在一个命令中指定我要查找的所有模式?
这是一个方法:
open my $fh, "<", "file.txt" or die $!;
while(my $line = <$fh>)
{
if ($line =~ m/errors:|mirror|raid\*|pond/)
{
next;
}
print $line;
}
close $fh;
既然你提到了 Perl,你可以使用:
perl -ne 'print unless /errors:|mirror|raid\*|pond/'
您还可以使用 sed 或 grep:
sed '/errors:\|mirror\|raid\*\|pond/d'
或者用 re-v
erse grep:
grep -v 'errors:\|mirror\|raid\*\|pond'
您可以将 -i
nplace 标志添加到 sed 和 Perl
使用 egrep
作为 Solaris
不支持 -E, --extended-regexp
选项
egrep -Ev "errors:|mirror|raid*|pond" file
awk
的另一种方式
awk '!/errors:|mirror|raid*|pond/' file
感谢评论中的anishsane
建议,使用grep
和多个表达式,
grep -v -e "errors:" -e "mirror" -e "raid*" -e "pond" file
我正在尝试找到一种方法来使用 perl 删除包含以下内容的行:
errors:
mirror
raid*
pond
我找到了:
perl -pi -e "s,errors:,,"
有没有一种方法可以在一个命令中指定我要查找的所有模式?
这是一个方法:
open my $fh, "<", "file.txt" or die $!;
while(my $line = <$fh>)
{
if ($line =~ m/errors:|mirror|raid\*|pond/)
{
next;
}
print $line;
}
close $fh;
既然你提到了 Perl,你可以使用:
perl -ne 'print unless /errors:|mirror|raid\*|pond/'
您还可以使用 sed 或 grep:
sed '/errors:\|mirror\|raid\*\|pond/d'
或者用 re-v
erse grep:
grep -v 'errors:\|mirror\|raid\*\|pond'
您可以将 -i
nplace 标志添加到 sed 和 Perl
使用 egrep
作为 Solaris
不支持 -E, --extended-regexp
选项
egrep -Ev "errors:|mirror|raid*|pond" file
awk
awk '!/errors:|mirror|raid*|pond/' file
感谢评论中的anishsane
建议,使用grep
和多个表达式,
grep -v -e "errors:" -e "mirror" -e "raid*" -e "pond" file