3

How to replace, from command-line, the content between <!-- Analytics code start --> and <!-- Analytics code end --> of index.html:

<html> ... <!-- Analytics code start --> ... <!-- Analytics code end --> </body> </html> 

by the content of a file, say myanalytics.txt ?

Note: I want to do it by command-line because I need to do it for many files.

4
  • you wan't to change the content between these two lines ??
    – Sidahmed
    CommentedJan 3, 2017 at 18:11
  • Sorry @Sidahmed what did you mean?
    – Basj
    CommentedJan 3, 2017 at 18:23
  • lets say <!-- Analytics code start --> is at the line 4, and <!-- Analytics code end --> is the line 10. Do you wanna change the lines 4 and 10, or the lines between 4 and 10 ??
    – Sidahmed
    CommentedJan 3, 2017 at 19:00
  • @Sidahmed The lines between 4 and 10.
    – Basj
    CommentedJan 3, 2017 at 19:48

3 Answers 3

2

A solution with awk could be:

awk '/Analytics code start/ { t=1; print; system("cat myanalytics.txt") } /Analytics code end/ { t=0 } t==0 { print } ' index.html 

(I indented the code to increase readability, but it can easily be a oneline)

Briefly explanation:

  • When t==0 always print the current line
  • If the current line matches "Analytics code start" set t==1 and then print the current line and the desired file with system("cat myanalytics.txt")
  • Now t is equal to 1 so the current line is never printed, but when the current line matches "Analytics code end", t is set to 0 again, so print the current line from now on.

Note:

This will not edit your file index.html. To modify index.html you can:

  • redirect the output from awk to a temporary file, and then use a command likemv or cp

  • use sponge from moreutils package, as follow:

    awk '[.. commands like above ..]' index.html | sponge index.html 
    0

    Given your situation, here's how you could do this:

    #!/bin/sh SOURCE=index.html if ! test -f "$SOURCE"; then echo source is missing >&2 exit 1 fi headLimit=`grep -n '<!-- Analytics code start -->' "$SOURCE" | cut -d: -f1` tailLimit=`grep -n '<!-- Analytics code end -->' "$SOURCE" | cut -d: -f1` linennr=`awk '{}END{print NR + 1}' $SOURCE` if ! test "$headLimit" -gt 0 -a "$tailLimit" -gt 0 >/dev/null; then echo something is wrong >&2 exit 2 fi ( head -n$headLimit "$SOURCE" echo "the stuff I want to inject in place of my analytics code" tail -n`expr $linennr - $tailLimit` "$SOURCE" ) >"$SOURCE.new" if test -s "$SOURCE.new"; then mv "$SOURCE" "$SOURCE.old" mv "$SOURCE.new" "$SOURCE" fi exit 0 

    Note that any ed based solution would allow to actually replace in-file, instead of generate a replacement one, ... Which would arguably be cleaner.

    1
    • Thanks! It seems complex, I thought it could be done in one line, don't you think it's possible?
      – Basj
      CommentedJan 3, 2017 at 21:10
    0

    i think this maybe work

    sed -rn '/<!-- Analytics code start -->/{ p; :X n; /<!-- Analytics code end -->/{p;b}; bX }; p' 

      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.