0

I want to replace the string

/opt/local/lib/ruby/gems/1.8/gems/cucumber-0.3.99/lib/cucumber.rb

with the string

/opt/local/lib/ruby/gems/1.8/gems/cucumber-0.3.99/lib/

on the command line, probably using sed. I can't for the life of me figure out the replacement regex to pass to sed. Or maybe sed isn't even the right tool for the job. Any help would be appreciated.

8

no need for sed:

dirname /usr/local/bin/program

will return /usr/local/bin

  • There is not even any need for dirname :-) . If the string is stored in $MYPROGRAM, then ${MYPROGRAM%/*} strips everything including and after the last slash, leaving you with the path to MYPROGRAM. This expansion behaviour is defined by POSIX. dirname handles relative paths and things like multiple slashes better, though. – Daniel Andersson Mar 28 '12 at 10:52
  • Neat! Which version of bash does it need to work? dirname is old and standard enough that it will work just about anywhere. – Marcin Mar 28 '12 at 15:21
  • @Marcin: it works with any Bash version; it is supposed to work with every standards (=POSIX) honoring shell. Also, on a separate note, if you respond to someone among the comments: if you start the comment with "@username", that user will be notified of the response through the site (I just found my comment by chance now, a week later). – Daniel Andersson Apr 4 '12 at 10:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.