0

I want to search and replace the negative values with the value just before it. The values are separated by comma.

Example:

ocv_sigma_cell_rise(table_7) { sigma_type : early_and_late; values("0.00616, 0.00505, 0.00337, -0.00026"); } 

In this I need to replace negative value "-0.00026" with the value just before it(here "0.00337").

The negative values need to be searched in a file with "ocv" as a common word.

2
  • Are lists of numbers always surrounded by double quotes like in your example? Are there other constraints that make it easier to parse such lines?CommentedFeb 17, 2021 at 5:42
  • Is this a contrived sample or a realistic one? Meaning can there be other data within this block or always two lines. What if the first value is negative or whatif all are negative?
    – guest_7
    CommentedFeb 17, 2021 at 5:57

1 Answer 1

0

For this restricted ocv .lib file you can run this small perl code.

perl -lne ' BEGIN { $arc = qr/ocv_sigma_cell_(rise|fall)\([^)]*\)\s*\{/; $num = qr/\d+(?:\.\d*)?|\.\d+/; } next unless /$arc/ ... /\}/; push @A,$_;next unless /\}/; my $var = join "\n", splice @A; $var =~ s{^\s*values\(\K(.*?)(?=\))} [ local $_ = $1; 1 while s/(?:"|\s)($num),\s+\K-$num(?=,|")/$1/; $_; ]xmse; print $var; ' your_ocv.lib 

Output:

ocv_sigma_cell_rise(table_7) { sigma_type : early_and_late; values("0.00616, 0.00505, 0.00337, 0.00337"); } 

    You must log in to answer this question.

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.