-1

New to sed, so trying to figure out following problem.

sed -e "s/menuentry \"test.*(/menuentry \"test image ${VERSION} (/" \
    -e "s/menuentry \"Lost password change.*(/menuentry \"Lost password change ${VERSION} (/" \
    -e "sX/boot/[A-Za-z0-9\.\-\@_]*X/boot/${VERSION}Xg" \
    < test.txt

Above expression, the 3rd part

-e "sX/boot/[A-Za-z0-9\.\-\@_]*X/boot/${VERSION}Xg" gives an error 

sed: -e expression #3, char 37: unknown option to `s' if $VERSION=FIX i.e contains "X".

What is the better way to deal with this? use another character as a delimiter, i tried ":" and it worked.

user1060517
  • 355
  • 1
  • 5
  • 17
  • 3
    Well, as you say, you can get around this using a different delimiter. There's your answer. – elixenide Dec 15 '14 at 04:36
  • 1
    why'd you use double quotes? If you're not going to reference some like a variable or a cmd (e.g. `$(CMD)` or `$VAR`) then you'd probably be better off using single quotes: `sed '/regex/'`. That way you don't have ugly nested double quotes. Don't use double quotes unless you _need_ them. – Alexej Magura Dec 15 '14 at 04:43
  • other delimiter right or escape the delimiter character in your variable (using a temporary variable or `$( )` or a shell variable modification during the substitution – NeronLeVelu Dec 15 '14 at 06:56

1 Answers1

1

awk to the rescue!

VERSION=FIX

awk -v VERSION="$VERSION" \
    '{
       sub (/menuentry "test.*\(/                , "menuentry \"test image " VERSION " ("          );
       sub (/menuentry "Lost password change.*\(/, "menuentry \"Lost password change " VERSION " (");
       gsub("/boot/[A-Za-z0-9.@_-]*"             , "/boot/" VERSION                                );
       print;
     }' \
    < foo.txt

Substituting variables into sed commands is Not A Good Idea™, precisely for the reasons you encountered. When you find yourself tempted to do that, consider using awk instead. It's less sexy, but it does not have this particular problem.

Wintermute
  • 42,983
  • 5
  • 77
  • 80