Quantcast
Channel: Using sed to replace the hexadecimal code for URL and to insert new SVG codes after SVG tag in all SVG files - Unix & Linux Stack Exchange
Viewing all articles
Browse latest Browse all 2

Answer by terdon for Using sed to replace the hexadecimal code for URL and to insert new SVG codes after SVG tag in all SVG files

$
0
0

If I understand correctly, you want to do two things:

  1. Replace the #5c616c with url(#grad1).
  2. Insert these lines after the opening <svg ...> tag:

    <linearGradient id="grad1" x1="0%" y1="0%" x2="100%" y2="0%"><stop offset="0%" style="stop-color:rgb(255,255,0);stop-opacity:1" /><stop offset="100%" style="stop-color:rgb(255,0,0);stop-opacity:1" /></linearGradient>

Personally, I'd just do the whole thing in Perl:

#!/bin/perlmy $replacement=<<EoF;<linearGradient id="grad1" x1="0%" y1="0%" x2="100%" y2="0%"><stop offset="0%" style="stop-color:rgb(255,255,0);stop-opacity:1" /><stop offset="100%" style="stop-color:rgb(255,0,0);stop-opacity:1" /></linearGradient>EoF## This is just to fix SE's syntax highlighting /    my $foundSvg = 0;while (<>) {  ## Insert the replacement after the 1st line matching '<svg'  if (/<\s*svg/) {    $foundSvg++;  }  if ($foundSvg == 1) {    ## $_ is the value of the current line. If we have found the <svg,    ## append $replacement to this line    $_ .= $replacement;    ## Increment $foundSvg so we don't do this twice    $foundSvg++;  }  ## For all lines, replace all occurrences of #5c616c with url(#grad1)  s/#5c616c/url(#grad1)/g;  ## Print the line  print;}

Save that as foo.pl and then:

for f in *svg; do   perl foo.pl "$f"> tmpFile && mv tmpFile "$f"done

Viewing all articles
Browse latest Browse all 2

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>