0

I have a simple file where I want to replace http://gems.ooyala.com with http://gems.sv2. My s command with sed works fine until I use -i.

Any hints?

(0)kashyap@vpn-client-223$ cat Gemfile
source "https://rubygems.org"
source "http://gems.ooyala.com"

gem "json-processing-profile"
gem "panache", ">= 0.1.3"
gem "pathological"
gem "sassy_gurl"
(0)kashyap@vpn-client-223$ 
(0)kashyap@vpn-client-223$ 
(0)kashyap@vpn-client-223$ sed 's~source "http://gems.ooyala.com"~source "http://gems.sv2"~g' Gemfile
source "https://rubygems.org"
source "http://gems.sv2"              <--- updated as expected..

gem "json-processing-profile"
gem "panache", ">= 0.1.3"
gem "pathological"
gem "sassy_gurl"
(0)kashyap@vpn-client-223$ 
(0)kashyap@vpn-client-223$ 
(0)kashyap@vpn-client-223$ sed -i 's~source "http://gems.ooyala.com"~source "http://gems.sv2"~g' Gemfile
sed: 1: "Gemfile": extra characters at the end of G command
(1)kashyap@vpn-client-223$
(0)kashyap@vpn-client-223$

Some more experiments to see what sed is thinking if it helps:

(0)kashyap@vpn-client-223$ cd ..
(0)kashyap@vpn-client-223$ sed -i 's~source "http://gems.ooyala.com"~source "http://gems.sv2"~g' my_test/Gemfile
sed: 1: "my_test/Gemfile": invalid command code o
(1)kashyap@vpn-client-223$ cd -
/Users/kashyap/repos/ots-system-test
(0)kashyap@vpn-client-223$ sed -i 's~source "http://gems.ooyala.com"~source "http://gems.sv2"~g' ../my_test/Gemfile
sed: 1: "../my_test/Gemfile": invalid command code .
(1)kashyap@vpn-client-223$

I'm on Mac, El Capitan (Mac OS X 10.11.x).

(0)kashyap@vpn-client-223$ which sed
/usr/bin/sed
(0)kashyap@vpn-client-223$ file /usr/bin/sed
/usr/bin/sed: Mach-O 64-bit executable x86_64
(0)kashyap@vpn-client-223$

I of course tried other separators with same result:

sed -i 's/source "http:\/\/gems.ooyala.com"/source "http:\/\/gems.sv2"/g' Gemfile
sed -i 's#source "http://gems.ooyala.com"#source "http://gems.sv2"#g' Gemfile
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Kashyap
  • 15,354
  • 13
  • 64
  • 103
  • 1
    If you're using homebrew you can install `gnu-sed`. – Turn Dec 31 '15 at 20:16
  • 1
    Now you know why standards like POSIX are useful. Of course, POSIX does not specify the `-i` option, and different tool sets do things differently when there isn't a standard to follow. – Jonathan Leffler Dec 31 '15 at 21:32
  • @JonathanLeffler yeah.. Wasted almost an hour including looking at the section describing `s` in sed manual. Just didn't read the `-i` section. – Kashyap Dec 31 '15 at 22:43
  • @shellter: No: Mac `sed` requires `sed -i '' 's/…/…/'` with the empty argument separate because the shell removes the two quotes attached to the `-i` in `-i""` and `sed` doesn't see anything there (it gets `-i` and uses the next argument as the suffix). To be portable between GNU and BSD variants, you must use an explicit, non-null backup suffix attached to the `-i` option: `-i.bak`. GNU `sed` requires the optional suffix attached to the `-i`; BSD `sed` requires an empty following argument for the null suffix, or accepts a non-empty suffix attached to the `-i` or in the following argument. – Jonathan Leffler Dec 31 '15 at 22:48
  • @JonathanLeffler : doah. I don't have a Mac, so I don't experience that subtlety on a daily basis. Thanks for the reminder. Removing my comment, yours is much better. – shellter Dec 31 '15 at 23:15

0 Answers0