Python_geek
Python_geek

Reputation: 45

Perl to delete specific lines

I have file which has more than 1000 lines, some lines have

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

I need to add .*

key="chicago_newyork_plane.*80Bs111010110101101011010110101101011010111001100111010111001100111100110011100111\.com_der_compare,chicago_newyork_plane.*80Bs111010110101101011010110101101011010111001100111010111001100111100110011100111
key="delhi_pune_plane.*Bs100010110101101011010110101101011010111001100111010111001100111100110011100111\.com_der_compare,delhi_pune_plane.*Bs100010110101101011010110101101011010111001100111010111001100111100110011100111

I have written code as,

$_ =~s/key="[a-z]+_[a-z]+_[a-z]+_[0-90]+_[0-9]+Bs/key=" [a-z]+_[a-z]+_[a-z]+.*/g;

I am unable to override that. I was able to do if i hard code the values or number of underscore (_) , but don't want to do so

Upvotes: 1

Views: 68

Answers (2)

sergiotarxz
sergiotarxz

Reputation: 540

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

or

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

Does the trick for me, I supposed the first result you said you want as the correct one.

Upvotes: 1

Stefan Becker
Stefan Becker

Reputation: 5972

If I understand your input data correctly

  • word character (\w -> KEEP)
  • followed by one-or-more underscore + number ((?:_\d+)+ -> REPLACE)

then this should be the correct solution.

#!/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

Test run:

$ 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

I just noticed that your expected output is inconsistent. If you want instead

  • word character (\w -> KEEP)
  • followed by one-or-more underscore + number ((?:_\d+)+ -> REPLACE)
  • followed by underscore (_ -> REPLACE)
  • followed by the string 80B (-> KEEP)

then it should be:

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

Upvotes: 0

Related Questions