Perl 替换两个字符之间的文本行
我有一个bibtex文件,它是其他几个.bib文件的合并。在合并过程中,除一个重复条目外,所有重复条目都被注释掉,因此具有重复条目的所有案例如下所示。其中一些有20~30个条目被注释掉,使得一个100个引用文件有30k行的文本Perl 替换两个字符之间的文本行,perl,text,replace,awk,sed,Perl,Text,Replace,Awk,Sed,我有一个bibtex文件,它是其他几个.bib文件的合并。在合并过程中,除一个重复条目外,所有重复条目都被注释掉,因此具有重复条目的所有案例如下所示。其中一些有20~30个条目被注释掉,使得一个100个引用文件有30k行的文本 @Article{goodnight2005, author = {Goodnight, N. and Wang, R. and Humphreys, G.}, journal = {{IEEE Computer Graphics and Applica
@Article{goodnight2005,
author = {Goodnight, N. and Wang, R. and Humphreys, G.},
journal = {{IEEE Computer Graphics and Applications}},
title = {{Computation on programmable graphics hardware}},
year = {2005},
volume = {25},
number = {5},
pages = {12-15}
}
###Article{goodnight2005,
author = {Goodnight, N. and Wang, R. and Humphreys, G.},
journal = {{IEEE Computer Graphics and Applications}},
title = {{Computation on programmable graphics hardware}},
year = {2005},
volume = {25},
number = {5},
pages = {12-15}
}
@INPROCEEDINGS{Llosa-pact96,
author = {Josep Llosa and Antonio González and Eduard Ayguadé and Mateo Valero},
title = {Swing Modulo Scheduling: A Lifetime-Sensitive Approach},
booktitle = {In IFIP WG10.3 Working Conference on Parallel Architectures and Compilation Techniques (PACT'96},
year = {1996},
pages = {80--86}
}
如何删除以####inclusive开头直到下一行以@exclusive开头的所有行?本质上,我的结果文件是:
@Article{goodnight2005,
author = {Goodnight, N. and Wang, R. and Humphreys, G.},
journal = {{IEEE Computer Graphics and Applications}},
title = {{Computation on programmable graphics hardware}},
year = {2005},
volume = {25},
number = {5},
pages = {12-15}
}
@INPROCEEDINGS{Llosa-pact96,
author = {Josep Llosa and Antonio González and Eduard Ayguadé and Mateo Valero},
title = {Swing Modulo Scheduling: A Lifetime-Sensitive Approach},
booktitle = {In IFIP WG10.3 Working Conference on Parallel Architectures and Compilation Techniques (PACT'96},
year = {1996},
pages = {80--86}
}
例如,sed'/######/,/@/{/!d}书目。bib保持以####开头的行,但是sed'/####/,/@/d'书目。bib使行以@go away开头
非常感谢您的帮助。使用
$skip
sentinel值的简单解决方案:
use strict;
use warnings;
my $skip = 0;
while ( <> ) {
$skip = 1 if /^###/;
$skip = 0 if /^@/;
next if $skip;
print;
}
使用严格;
使用警告;
我的$skip=0;
而(){
$skip=1如果/^####/;
$skip=0如果/^@;
下一步如果$skip;
打印
}
输出:
[hmcmillen]$ perl test.pl < test.txt
@Article{goodnight2005,
author = {Goodnight, N. and Wang, R. and Humphreys, G.},
journal = {{IEEE Computer Graphics and Applications}},
title = {{Computation on programmable graphics hardware}},
year = {2005},
volume = {25},
number = {5},
pages = {12-15}
}
@INPROCEEDINGS{Llosa-pact96,
author = {Josep Llosa and Antonio González and Eduard Ayguadé and Mateo Valero},
title = {Swing Modulo Scheduling: A Lifetime-Sensitive Approach},
booktitle = {In IFIP WG10.3 Working Conference on Parallel Architectures and Compilation Techniques (PACT'96},
year = {1996},
pages = {80--86}
}
[hmcmillen]$perl test.pl
如果确实希望它是单个命令:
perl -ne 'BEGIN { $SKIP = 1 } $SKIP = 1 if /^###/; $SKIP = 0 if /^@/; print unless $SKIP;' < test.txt
perl-ne'BEGIN{$SKIP=1}$SKIP=1 if/^###/$SKIP=0 if/^@/;除非$SKIP;,否则打印
假设您的输入文件都是当前目录或更低目录中的*.bib
文件
让我成为你今天的寻找perl
魔术师:
find . -name '*.bib' -exec \
perl -i -ne '$o=1if/^@/;$o=0if/^###/;print if$o' \{} \;
如果你不能阅读,不要使用它。例如,它会在第一个<代码> @代码>代码行之前删除任何东西,并且不会考虑缩进<代码> @ <代码>或<代码> > <代码> >行。< /P>
还有一个很好的模块叫做
文件::查找
,请使用perldoc文件::查找
,阅读所有关于它的内容。就个人而言,它不会将此作为一行代码保存。使用awk:
$ awk '/###/{p=0} /@/{p=1} p' bib.text
@Article{goodnight2005,
author = {Goodnight, N. and Wang, R. and Humphreys, G.},
journal = {{IEEE Computer Graphics and Applications}},
title = {{Computation on programmable graphics hardware}},
year = {2005},
volume = {25},
number = {5},
pages = {12-15}
}
@INPROCEEDINGS{Llosa-pact96,
author = {Josep Llosa and Antonio González and Eduard Ayguadé and Mateo Valero},
title = {Swing Modulo Scheduling: A Lifetime-Sensitive Approach},
booktitle = {In IFIP WG10.3 Working Conference on Parallel Architectures and Compilation Techniques (PACT'96},
year = {1996},
pages = {80--86}
}
“a@独家”是什么意思?到目前为止,您尝试了什么?尝试阅读以获得更好的体验。理解inclusive?Exclusive没有问题。我的意思是删除第一个#####之间的所有行,直到它到达@。将保留以@开头的行。我看到了几个sed示例,我经常使用它们来删除/替换st环,但不删除行,没有一个适合我的需要。你尝试过什么?看到BibTeX解析器也可以用来剥离注释,看到例如:这就像是一个一次性作业,其中Text::BibTeX可能会有过多的杀伤力,不是吗?嗯,我希望有一个更短的sed命令左右。但这是肯定的。对不起,answ时没有看到输出框er通过了。超级。你从来没有要求sed解决方案。你只是提到你尝试了一个sed命令作为解决方案。我刚刚刷新了页面,看到了第一个代码框,但没有看到其余的代码框,我错误地认为它是java。然后在我发布了注释后,我看到了perl命令和答案上显示的输出框。sed不是必需的恩,你说得对。我总是有点慢。但实际上,perl-I-ne
是我的方法。我通常喜欢在替换之前验证是否执行了正确的替换,所以我通常使用-I.bak
我只是在这个问题上省略了它