Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How can I improve pwd | sed -e 's/.*\/\(.*\)\/\(.*\)\/\(.*\)/...\1\/\2\/\3/' to only return the ellipsis if pwd is greater than three levels deep?

The goal is to avoid ...home/example/foo, but still receive ...example/foo/bar.

share|improve this question
up vote 3 down vote accepted
pwd | sed -e 's/\/.*\/\(.*\)\/\(.*\)\/\(.*\)/...\1\/\2\/\3/'

A tip for readability is to use another regex delimiter character when you need to use a lot of slashes:

pwd | sed -e 's#/.*/\(.*\)/\(.*\)/\(.*\)#...\1/\2/\3#'
share|improve this answer
    
read his question again man. By the way, sed -r would make it neater he can replace his \( with ( but look at what he's asking. – barlop Jun 13 '12 at 5:32
1  
@barlop: It works here. I get /home/daniel/foo but ...daniel/foo/bar, which seems to be exactly what he wants. At a first glance I think it is you who should read the question again. EDIT: at a second, third and fourth glance, I still think it is you who should read the question again :-) – Daniel Andersson Jun 13 '12 at 5:36
    
@barlop: No. In his example he would have gotten the ellipsis if pwd was >=3 levels deep, he wanted this to be changed to >3. Try the command locally to see how it behaves. EDIT: This answered a now removed comment. – Daniel Andersson Jun 13 '12 at 5:44
    
sorry.. you're right. you fixed it functionally and made it neater – barlop Jun 13 '12 at 5:44
    
@barlop: No worries. At least here it's very early, so I might have written anything at this point of day, so you have every reason to be suspicious :-D – Daniel Andersson Jun 13 '12 at 5:47

You must log in to answer this question.

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