user1661161
user1661161

Reputation: 13

Replace data after a regex pattern

I have a log file with data I need to mask/replace.

Test file example

20120910:181649:TID=000ef4:Add       :C155:E076:P:     eTDYN-str-multi-109(String to replace)
20120910:181649:TID=000ef4:Add       :C155:E076:P:     eTDYN-str-multi-106(String to replace)
20120910:181649:TID=000ef4:Add       :C155:E076:P:     eTDYN-str-multi-104(String to replace)
20120910:181649:TID=000ef4:Add       :C155:E076:P:     eTDYN-str-multi-102(String to replace)
20120910:181649:TID=000ef4:Add       :C155:E076:P:     eTDYN-str-multi-18(String to replace)

It has many more of the eTDYN-str, 01-110.

I need to find each instance, one per line, and replace all of the text directly after it.

I was trying something like

perl -pie 's/^(eTDYN-str-multi-\d\d:\s+).*/$1<removed pii data>/g;' logtest.txt

I know it would only work for two digit numbers after the text, but it's not even finding/replacing it.

Upvotes: 1

Views: 1025

Answers (2)

Josh Y.
Josh Y.

Reputation: 876

Though this is resolved, I'm posting a Perl solution for posterity:

First, the -pie flags are treated as -p -ie and not -p -i -e as you intended, because the -i flag takes an argument, the extension of the backup file for in-place editing. -i by itself disables the backup. But anyway:

$ pbpaste|perl -pe's/eTDYN-str-multi-\d{1,3}\K.*/<removed pii data>/'
20120910:181649:TID=000ef4:Add       :C155:E076:P:     eTDYN-str-multi-109<removed pii data>
20120910:181649:TID=000ef4:Add       :C155:E076:P:     eTDYN-str-multi-106<removed pii data>
20120910:181649:TID=000ef4:Add       :C155:E076:P:     eTDYN-str-multi-104<removed pii data>
20120910:181649:TID=000ef4:Add       :C155:E076:P:     eTDYN-str-multi-102<removed pii data>
20120910:181649:TID=000ef4:Add       :C155:E076:P:     eTDYN-str-multi-18<removed pii data>

Makes use of the \K or 'keep' escape, documented in perlre:

For various reasons \K may be significantly more efficient than the equivalent (?<=...) construct, and it is especially useful in situations where you want to efficiently remove something following something else in a string. For instance

s/(foo)bar/$1/g;

can be rewritten as the much more efficient

s/foo\Kbar//g;

Upvotes: 0

fo_x86
fo_x86

Reputation: 2613

Use sed to do something like:

sed 's/\(eTDYN-str-multi-[0-9]\{1,3\}\).*/\1<REPLACEMENT_STRING>/' <name of your test file>

This will match the literal "eTDYN-str-multi-" followed by one, two, or three digits, save that as "\1" and will match anything after that. Then it'll replace the matched string with "\1" (the saved portion and of your choice.

Upvotes: 1

Related Questions