Perl删除特定行
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Perl删除特定行相关的知识,希望对你有一定的参考价值。
我有超过1000行的文件,有些行有
key="chicago_newyork_plane_1_3_8_7_9_80Bs111010110101101011010110101101011010111001100111010111001100111100110011100111.com_der_compare,chicago_newyork_plane_1_3_8_7_9_80Bs111010110101101011010110101101011010111001100111010111001100111100110011100111
key="delhi_pune_plane_1_3_8_7_15_16_10_11_9_80Bs100010110101101011010110101101011010111001100111010111001100111100110011100111.com_der_compare,delhi_pune_plane_1_3_8_7_15_16_10_11_9_80Bs100010110101101011010110101101011010111001100111010111001100111100110011100111
我需要补充一下。*
key="chicago_newyork_plane.*80Bs111010110101101011010110101101011010111001100111010111001100111100110011100111.com_der_compare,chicago_newyork_plane.*80Bs111010110101101011010110101101011010111001100111010111001100111100110011100111
key="delhi_pune_plane.*Bs100010110101101011010110101101011010111001100111010111001100111100110011100111.com_der_compare,delhi_pune_plane.*Bs100010110101101011010110101101011010111001100111010111001100111100110011100111
我写了代码,
$_ =~s/key="[a-z]+_[a-z]+_[a-z]+_[0-90]+_[0-9]+Bs/key=" [a-z]+_[a-z]+_[a-z]+.*/g;
我无法覆盖它。如果我硬编码下划线(_)的值或数字,我就能做到,但不想这样做
答案
while (<$fd>) {
s/wK_(?:d+_)+/.*/;
say;
}
要么
while (<$fd>) {
s/(?<=w)_(?:d+_)+/.*/;
say;
}
对我来说,诀窍,我认为你说的第一个结果是正确的。
另一答案
如果我正确理解您的输入数据
- 字符(
w
- > KEEP) - 然后是一个或多个下划线+数字(
(?:_d+)+
- > REPLACE)
那么这应该是正确的解决方案。
#!/usr/bin/perl
use warnings;
use strict;
while (<DATA>) {
s/(?<=w)(?:_d+)+/.*/g;
print;
}
exit 0;
__DATA__
key="chicago_newyork_plane_1_3_8_7_9_80Bs111010110101101011010110101101011010111001100111010111001100111100110011100111.com_der_compare,chicago_newyork_plane_1_3_8_7_9_80Bs111010110101101011010110101101011010111001100111010111001100111100110011100111
key="delhi_pune_plane_1_3_8_7_15_16_10_11_9_80Bs100010110101101011010110101101011010111001100111010111001100111100110011100111.com_der_compare,delhi_pune_plane_1_3_8_7_15_16_10_11_9_80Bs100010110101101011010110101101011010111001100111010111001100111100110011100111
测试运行:
$ perl dummy.pl
key="chicago_newyork_plane.*Bs111010110101101011010110101101011010111001100111010111001100111100110011100111.com_der_compare,chicago_newyork_plane.*Bs111010110101101011010110101101011010111001100111010111001100111100110011100111
key="delhi_pune_plane.*Bs100010110101101011010110101101011010111001100111010111001100111100110011100111.com_der_compare,delhi_pune_plane.*Bs100010110101101011010110101101011010111001100111010111001100111100110011100111
我刚刚注意到你的预期输出是不一致的。如果你想要的话
- 字符(
w
- > KEEP) - 然后是一个或多个下划线+数字(
(?:_d+)+
- > REPLACE) - 接着是下划线(
_
- > REPLACE) - 后跟字符串
80B
( - > KEEP)
那应该是:
s/(?<=w)(?:_d+)+_(?=80B)/.*/g;
以上是关于Perl删除特定行的主要内容,如果未能解决你的问题,请参考以下文章