Search code examples
linuxsvgsedreplacelinux-mint

Find and replace function that will do simple arithmetic?


I am generating simple SVG diagrams by using sed find and replace on some text input. However, I need a more sophisticated find and replace operation involving simple math, to alter certain X and Y values.

E.g. I need to multiply all Y values by a factor of 0.5 or 0.2 or 0.

Because of how I want this to work, it cannot be achieved with a transform operation within the SVG.*

I just need to be able to find, say, all instances of ([0-9.]*)VERT (in sed speak) and replace with the mathematical result of \1 multiplied by the constant I choose.


Solution

  • You could write a simple perl script. Not sure what determines $factor, but this should at least give you a running start.

    #! /usr/bin/perl
    my $factor=0.5;
    while(my $line=<>) {
        if ($line =~ /([0-9.]*)VERT/) {
            my $num = $factor * $1;
            $line =~ s/([0-9.]*)VERT/${num}VERT/;
        }
        print $line;
    }
    

    Usage: ./scriptname.pl <file_to_process.txt;