Regex 匹配一些行并用Perl删除它们

Regex 匹配一些行并用Perl删除它们,regex,perl,Regex,Perl,问题: Title[nameofdevice:cpu]: nameofdevice CPU Usage Mhz Target[nameofdevice:cpu]: MaxBytes[nameofdevice:cpu]: 100 Options[nameofdevice:cpu]: gauge WithPeak[nameofdevice:cpu]: wmy YLegend[nameofdevice:cpu]: Percent ShortLegend[nameofdevice:cpu]: % Leg

问题

Title[nameofdevice:cpu]: nameofdevice CPU Usage Mhz
Target[nameofdevice:cpu]:
MaxBytes[nameofdevice:cpu]: 100
Options[nameofdevice:cpu]: gauge
WithPeak[nameofdevice:cpu]: wmy
YLegend[nameofdevice:cpu]: Percent
ShortLegend[nameofdevice:cpu]: %
Legend1[nameofdevice:cpu]: CPU % for 1
Legend2[nameofdevice:cpu]: CPU Max for 2
Legend3[nameofdevice:cpu]: Max CPU Mhz for 1
Legend4[nameofdevice:cpu]: Max CPU Mhz for 2

Title[nameofdevice:mem]: nameofdevice mem
Target[nameofdevice:mem]:
MaxBytes[nameofdevice:mem]: 100
Options[nameofdevice:mem]: gauge
WithPeak[nameofdevice:mem]: wmy
YLegend[nameofdevice:mem]: Percent
ShortLegend[nameofdevice:mem]: %
Legend1[nameofdevice:mem]: % Used
Legend2[nameofdevice:mem]: Max Used
Legend3[nameofdevice:mem]: Max
许多文件name.CFG中有一些错误的行(大约2700个文件)。我需要删除坏线,如果他们存在

错误行包含文件名减去.CFG

不好的线条

Title[nameofdevice:cpu]: nameofdevice CPU Usage Mhz
Target[nameofdevice:cpu]:
MaxBytes[nameofdevice:cpu]: 100
Options[nameofdevice:cpu]: gauge
WithPeak[nameofdevice:cpu]: wmy
YLegend[nameofdevice:cpu]: Percent
ShortLegend[nameofdevice:cpu]: %
Legend1[nameofdevice:cpu]: CPU % for 1
Legend2[nameofdevice:cpu]: CPU Max for 2
Legend3[nameofdevice:cpu]: Max CPU Mhz for 1
Legend4[nameofdevice:cpu]: Max CPU Mhz for 2

Title[nameofdevice:mem]: nameofdevice mem
Target[nameofdevice:mem]:
MaxBytes[nameofdevice:mem]: 100
Options[nameofdevice:mem]: gauge
WithPeak[nameofdevice:mem]: wmy
YLegend[nameofdevice:mem]: Percent
ShortLegend[nameofdevice:mem]: %
Legend1[nameofdevice:mem]: % Used
Legend2[nameofdevice:mem]: Max Used
Legend3[nameofdevice:mem]: Max
nameofdevice
是文件名减去.CFG

我正在寻找一种linux方式来实现这一点,但Perl似乎更灵活。我真正的问题是与文本完全匹配。我想有多行和变量是让我困惑的,但是一个小字符串你可以做一个查找和替换之类的事情。或者使用
SED


我需要删除所有文件中的错误行。

使用
grep-p
(PCRE):

要跳过这些行,请执行以下操作:

grep -v -P 'nameofdevice(?!\.CFG)' file > tmpFile
mv tmpFIle file
这会起作用(尽管我相信有一种更有效的方法):

如果您在windows上,则需要为inplace(
-i
)功能提供备份文件名:

find-iname'*.cfg'
查找当前目录和所有子目录中的每个
.cfg
文件

Perl脚本是:

# there's an implied while(<>) because of the -n option
# which executes this script for each line

# $ARGV is the current file path, strip ./ and .cfg
$f = $ARGV =~ s#\./|\.cfg##gr;

# print the current line if it doesn't contain the current file name
# as a whole word
print if !m/\b\Q$f\E\b/;
#由于-n选项,有一个隐含的while()
#它为每一行执行这个脚本
#$ARGV是当前文件路径,strip./and.cfg
$f=$ARGV=~s\./\.cfg\\\gr;
#如果当前行不包含当前文件名,则打印当前行
#总而言之
打印如果!m/\b\Q$f\E\b/;
甚至

... print unless /\b\Q$n\E\b/

它将创建一个备份(file.cfg.bak)

如果您的Perl程序有问题,那么您应该显示它并描述出现了什么问题。你的问题不清楚。你是说
nameofdevice
应该是
nameofdevice.CFG
?在这种情况下,为什么不能直接更正行而不是删除行?我想你想删除整个区块直到下一个空行?如果您显示了一些有效的数据以及错误的条目,这会有所帮助。您是指这个
sed'/\bnameofdevice\b/d'文件
我需要从每个文件中删除这些行。除了设备名称外,它们完全相同,但我需要从每个CFG中删除坏行而不是删除名称如果文件中存在,则需要删除上面的整个块。您是否检查了我的
grep-v
命令,该命令在输出上打印除匹配行以外的所有行?我想我不明白这是如何删除整个CFG块的上面的文本?我对Perl脚本进行了注释-您不理解的部分是什么?上面的代码块在哪里,或者正则表达式正在删除它?我需要删除上面的整个文本块,但在每个文件中,设备名称都在那里,因此它正在更改。我只是在问问题。这将在当前目录和所有子目录中执行(如果只需要当前目录,请在
后添加
-maxdepth 1
查找
)。在执行像这样的大规模更新操作之前,请始终备份文件。我需要删除“坏行”上方的这段文本不删除名称。基本上,我需要加载文件,看看它是否包含上述错误行,以及是否删除它们。虽然在每个文件中,错误行包含每个文件的名称,但这正是一行的作用:)(或者我不理解问题)。建议:使用2或3个文件
perl。。。。。。file1.cfg file2.cfg
,计算差异
diff file1.cfg fil1.cfg.bak
# there's an implied while(<>) because of the -n option
# which executes this script for each line

# $ARGV is the current file path, strip ./ and .cfg
$f = $ARGV =~ s#\./|\.cfg##gr;

# print the current line if it doesn't contain the current file name
# as a whole word
print if !m/\b\Q$f\E\b/;
perl -i.bak -ne '$n=$ARGV=~s/\.cfg$//r; print unless /\b$n\b/' *.cfg
... print unless /\b\Q$n\E\b/