Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sed throws 'bad flag in substitute command'

I want to run a simple command of replacing absolute paths to relative ones inside a CSS file like this:

sed -i 's/\/fonts/../fonts/' /Users/sergeybasharov/WebstormProjects/snap/compiled/Content/stylesheets/style.css

It throws this

sed: 1: "/Users/sergeybasharov/W ...": bad flag in substitute command: 'b'

What can be wrong in this simple script?

like image 702
Sergei Basharov Avatar asked Jul 08 '13 19:07

Sergei Basharov


4 Answers

sed -i 's/\/fonts/../fonts/' is not a valid sed command, try sed -i 's#/fonts#../fonts#'

like image 37
Endoro Avatar answered Oct 23 '22 12:10

Endoro


In your command s/\/fonts/../fonts/ is being taken as the parameter to the -i option (the suffix to use for the backup file), and the filename argument is being treated as the editing commands.

You need to specify to disable the backup file creation:

sed -i '' ...

In your example:

sed -i '' 's/\/fonts/../fonts/' /Users/sergeybasharov/WebstormProjects/snap/compiled/Content/stylesheets/style.css

Computers are dumb, they don't figure things out by context, so they can't tell that something beginning with s/ is obviously an editing command, not a suffix.

like image 170
Barmar Avatar answered Oct 23 '22 11:10

Barmar


I was having a similar issue. You can install the GNU version of sed in your Mac, called gsed, and use it using the standard Linux syntax.

For that, install gsed using ports (if you don't have it, get it at http://www.macports.org/) by running sudo port install gsed. Then, you can run:

gsed -i 's/old_pattern/new_pattern/g' *
like image 21
toquart Avatar answered Oct 23 '22 12:10

toquart


in sed you can use any character to split, in my case I had / in one of the strings hence the error so I had to replace / with | in the sed split; hope this helps others

STRING1="something/somethingelse"
STRING2="clean"

sed -i '' "s|${STRING1}|${STRING2}|g" FILE
like image 2
Alexandru Avatar answered Oct 23 '22 13:10

Alexandru