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.

0

1 Answer 1

8

no need for sed:

dirname /usr/local/bin/program

will return /usr/local/bin

3
  • 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. Commented Mar 28, 2012 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
    Commented Mar 28, 2012 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). Commented Apr 4, 2012 at 10:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .