Perl删除特定行

问题描述 投票:1回答:2

我有超过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;

我无法覆盖它。如果我硬编码下划线(_)的值或数字,我就能做到,但不想这样做

perl
2个回答
1
投票
while (<$fd>) {
    s/\w\K_(?:\d+_)+/.*/;
    say;
}

要么

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

对我来说,诀窍,我认为你说的第一个结果是正确的。


0
投票

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

  • 字符(\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;
© www.soinside.com 2019 - 2024. All rights reserved.