capture till first occurrence.

Hi,

In my file I have following to lines
-------------------------------------------
C:/India/Apple/Mumbai/Red_Apple/Apple.txt

cd C:/India/Apple && run
-------------------------------------------

I want to replace "C:/India/Apple" with "D:/Test/Red_Apple" whereever the pattern "C:/India/Apple" occurs in my file.

what I want is to capture the first occurrence of "Apple" after "C:" in the line.

Please help me out.

I am trying
s/C:\/.*?Apple/D:\/Test\/Red_Apple/g

but it is replacing "C:/India/Apple/Mumbai/Red_Apple/Apple.txt" to "D:/Test/Red_Apple.txt"

Note: C:/India/Apple can be anything. Meaning it can be C:/India/Mango/Apple etc etc.

Should be something like:

sed 's!C:/India/Apple!D:/Test/Red_Apple!g' file

Thanks Franklin52 but I already knew this trick.

Please read query carefully. What I want is to capture first occurrence of "Apple" after "C:" in the line. between C:\ and \Apple there can be anything.
Hence the solution provided by you is applicable only in the case where there is C:\India\Apple.
But in my various files it would not always be C:\India\Apple, it can be anything like
C:/India/Mango/Apple or C:/Mumbai/Jaipur/Mango/Apple

I want a regex for this.

I did:

Pls post the test cases.

What would be the input and output your expecting clearly.

since you mentioned :

benitdhotekar

I am trying
s/C:\/.*?Apple/D:\/Test\/Red_Apple/g 

but it is replacing "C:/India/Apple/Mumbai/Red_Apple/Apple.txt" to "D:/Test/Red_Apple.txt"

wht is the Output your expecting in this case ?.

Hi Panyam,

The output expected is as follows:

D:/Test/Red_Apple/Mumbai/Red_Apple/Apple.txt

That means I want to replace the text starting from "C:\" till first occurrence of "\Apple\" by "D:/<some_dir>/Apple".
Between "C:\" and first occurrence of "\Apple" there can reside any text.

The output I am getting by using the above mentioned regex is that it takes the last occurrence of "Apple" in the line whereas I want the first occurrence of "Apple".

Thanks in Advance.

-----Post Update-----

Hi Franklin,
sorry for that but I meant to say to read the note carefully.
I am looking for a generic regex which would not search for a specific string but will capture till the first occurrence of the expected string i.e. "Apple" in this scenario.

sed -e 's|^C:/India/Apple|D:/TEST/RED_APPLE|' -e 's|^C:/India\(.*/Apple\)/\(.*\)|D:/TEST/RED_APPLE/\2|'

TEST:

TESTING>echo "C:/India/Apple/dd/d/d/d" |
> sed -e 's|^C:/India/Apple|D:/TEST/RED_APPLE|' -e 's|^C:/India\(.*/Apple\)/\(.*\)|D:/TEST/RED_APPLE/\2|'
D:/TEST/RED_APPLE/dd/d/d/d
TESTING>echo "C:/India/Apple/d/d/d/Apple.txt" |
> sed -e 's|^C:/India/Apple|D:/TEST/RED_APPLE|' -e 's|^C:/India\(.*/Apple\)/\(.*\)|D:/TEST/RED_APPLE/\2|'
D:/TEST/RED_APPLE/d/d/d/Apple.txt
TESTING>echo "C:/India/d/d/d/Apple/d/d/d/Apple.txt" |
> sed -e 's|^C:/India/Apple|D:/TEST/RED_APPLE|' -e 's|^C:/India\(.*/Apple\)/\(.*\)|D:/TEST/RED_APPLE/\2|'
D:/TEST/RED_APPLE/d/d/d/Apple.txt
TESTING>echo "C:/India/d/d/d/Apple/d/d/d/Apple" |
> sed -e 's|^C:/India/Apple|D:/TEST/RED_APPLE|' -e 's|^C:/India\(.*/Apple\)/\(.*\)|D:/TEST/RED_APPLE/\2|'
D:/TEST/RED_APPLE/d/d/d/Apple