Sonya777 has asked for the wisdom of the Perl Monks concerning the following question:

Hi experts,

I need help with an edit of a .txt file. I want to replace a part of the string with an empty space (i.e. remove the part of the string).

I have many lines like this one:

[{"absoluteLimits":{"conditionalLimits":[{"bidirVolume":4096000,"name" +:"Home"}],"resetPeriod":{"volume":"monthly day 15 00:??"}},"sliceVolu +me":5120,"subscriptionDate":"29-09-2016"},<b>{"absoluteLimits":{"cond +itionalLimits":[{"bidirVolume":102400000,"name":"Home"}],"resetPeriod +":{"volume":"2400 hours"}},"description":"Promotion_tariffbasic:29-09 +-2016,08-01-2017T10:21","name":"1004","sliceVolume":5120,"subscriptio +nDate":"30-09-2016T09:21"}]}

And from this line I want to remove the bolded string. To do this for many lines, basically I need to remove a part of the string that starts with:

{"absoluteLimits":{"conditionalLimits":[{"bidirVolume":102400000,"name +":"Home"}],"resetPeriod":{"volume":"2400 hours"}},"description":"Prom +otion_tariffbasic:

(this is the "common" part for each line, since the date is different for each line)

The problem is that the string contains special characters like ",:,} and a simple search & replace script does not work:

perl -p -e 's/{"absoluteLimits":{"conditionalLimits":[{"bidirVolume":1 +02400000,"name":"Home"}],"resetPeriod":{"volume":"2400 hours"}},"desc +ription":"Promotion_tariffbasic:.*//s' example.txt

Do you have any idea how this can be done?

Thanks a lot!


Replies are listed 'Best First'.
Re: Replace a string that contains special characters
by Corion (Patriarch) on Mar 21, 2017 at 16:08 UTC

    If you don't really care about leaving the JSON intact, your problem at hand seems to be that Perl regards { and [ and . as special within regular expressions.

    To temporarily switch off the special treatment, see the \Q and \E escapes in perlre. Basically, your regexp could be written as:

    s/\Q{"absoluteLimits":{"conditionalLimits":[{"bidirVolume":102400000," +name":"Home"}],"resetPeriod":{"volume":"2400 hours"}},"description":" +Promotion_tariffbasic:\E.*//s

    (find where I added \Q and \E in the Perl code)

Re: Replace a string that contains special characters
by stevieb (Canon) on Mar 21, 2017 at 16:09 UTC

    That's some JSON data that is, with an extra } at the end.

    Here's an example of how you can load that JSON data as a Perl data structure, then delete items from it, by name, then re-encode the data structure back into a JSON string. Note it's an array reference of hash references:

    use warnings; use strict; use JSON; my $json; { local $/; $json = <DATA>; } my $data = decode_json $json; for my $entry (@$data){ delete $entry->{absoluteLimits}; } $json = encode_json $data; print $json; __DATA__ [{"absoluteLimits":{"conditionalLimits":{"bidirVolume":4096000,"name": +"Home"},"resetPeriod":{"volume":"monthly day 15 00:??"}},"sliceVolume +":5120,"subscriptionDate":"29-09-2016"},{"absoluteLimits":{"condition +alLimits":{"bidirVolume":102400000,"name":"Home"},"resetPeriod":{"vol +ume":"2400 hours"}},"description":"Promotion_tariffbasic:29-09-2016,0 +8-01-2017T10:21","name":"1004","sliceVolume":5120,"subscriptionDate": +"30-09-2016T09:21"}]


    [{"subscriptionDate":"29-09-2016","sliceVolume":5120},{"name":"1004"," +subscriptionDate":"30-09-2016T09:21","sliceVolume":5120,"description" +:"Promotion_tariffbasic:29-09-2016,08-01-2017T10:21"}]
Re: Replace a string that contains special characters
by kennethk (Abbot) on Mar 21, 2017 at 16:00 UTC

    Please note that it's helpful to wrap inputs and outputs in code tags, in addition to code itself. Whitespace gets mangled and pairs of square brackets linkify content.

    Given that it looks like you are dealing with JSON, using the JSON module is probably the clean solution. You can parse the JSON, delete the offending keys, and then reserialize. It's not clear to me how one identifies the offending elements -- can you characterize the traits of the objects you wish to remove?

    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.