Perl删除特定行

时间:2019-02-15 06:06:21

标签: 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;

我无法覆盖它。 如果我对下划线(_)的值或数字进行硬编码,则可以执行此操作,但是不想这样做

2 个答案:

答案 0 :(得分:1)

while (<$fd>) {
    s/\w\K_(?:\d+_)+/.*/;
    say;
}

while (<$fd>) {
    s/(?<=\w)_(?:\d+_)+/.*/;
    say;
}

对我有用吗,我以为你说要得到的第一个结果就是正确的结果。

答案 1 :(得分:0)

如果我正确理解了您的输入数据

  • 文字字符(\w->保持)
  • 后跟一个或多个下划线+数字((?:_\d+)+->替换)

那么这应该是正确的解决方案。

#!/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->保持)
  • 后跟一个或多个下划线+数字((?:_\d+)+->替换)
  • 后跟下划线(_-> REPLACE)
  • 后跟字符串80B(-> KEEP)

那么应该是:

s/(?<=\w)(?:_\d+)+_(?=80B)/.*/g;