Need help in sed command

I have the input


and the output should be like:


The command i am using here is

$ echo bmb/eng/case.bmb | sed -E 's/.*/& &/;:a;s/([^ ]* [^n]*)(bm)b/12x/;ta;s~/~&eng&~2'
bmb/eng/eng/case.bmb bmx/eng/case.bmx

I have tied modifying the above command in multiple ways but i am not getting the expected output.

Asked By: afrin


First, it’s easier to do one thing at a time, so copy your line in the hold space with h, modify the line to produce the first output line, print it, then restore the save line with g and modify it to produce the second output line.

This allows you to have very simple replacements like s/eng./&/ for the first part or s_/.*/__ is the directory can have any name or could be core than one hierachy. And a simple s/bmb/bmx/ for the second line.

echo bmb/eng/case.bmb | sed 'h;s_/.*/_/_;p;g;s/bmb/bmx/g'

produces the desired output.

You can use the hold space like the clipboard of your word processor. It’s an extremely versatile and easy to use tool and such a pitty, that many sed users do not use it’s power.

It can be done in one single search-replace command.

echo 'bmb/eng/case.bmb'|sed -r 's:^(bm)b(/[^/]+)(/.*.1)b$:1b3bn1x23x:'

If you use () to group each matching piece, you can construct both lines of interest from those pieces, numbered 1, 2 and 3.

Answered By: seshoumara
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.