Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sed error: bad flag in substitute command: 'U'

Tags:

regex

bash

sed

I'm new in bash script and trying to replace some words in my file using sed. Following is the bash I use in my script:

sed -i '' "s/<pre>.*<\/pre>/<pre>($NEWNAME) $MD5<\/pre>/"~/Desktop/replace.html

And I got error message saying: bad flag in substitute command: 'U'. I use double quote because I need to put variables in.

My environment is Mac.

======================================

1.Turns out I forgot to leave a space between replace string and file name. Which led to the result always showing: bad flag in substitute command: '~'. It works now.

2.The reason is I used MD5=$(md5 path) to create MD5 value which gets the reault of MD5 (path) *****, and the path contains / which breaks the regex. After changing MD5=$(md5 -q path), it will be ok.

like image 697
JasmineOT Avatar asked Sep 16 '15 13:09

JasmineOT


1 Answers

Most likely your $NEWNAME variable has a forward slash in it, which is being used as regex delimiter in sed. Try this sed with an alternate delimiter e.g. ~:

sed -i '' "s~<pre>.*</pre>~<pre>($NEWNAME) $MD5</pre>~" ~/Desktop/replace.html
like image 137
anubhava Avatar answered Sep 17 '22 19:09

anubhava