Replace last 2 folder directory string with sed

Hi guys,

I�m trying to replace the 2 last folders name in a list of directories with a new string, but I�m don�t know which regex to apply.

Directories list:

C/my user/documents/games & music
C/my user/documents/photos 09-24-2008
C/my user/settings/config ?1_2

* The last folder may have characters such as -, _, & or ?

Example with the firt line

sed 's/\/documents\/games & music/\/final folder/g' infile

Desired result is as follow:

C/my user/final folder
C/my user/final folder
C/my user/final folder

Any help would be very appreciated.

use

sed 's/\(.*\/\)[^/]*$/\1final folder/g' filename

cheers,
Devaraj Takhellambam

you can try this..but :confused:

awk  -F/ 'BEGIN{OFS="/"}{ $NF="final folder";print }' file_name 
C/my user/documents/final folder
C/my user/documents/final folder
C/my user/settings/final folder
final folder
final folder
final folder

Why the last three line coming here???:frowning:

Many thanks devtakh,

It works partially for me, I�m not sure if because the shell I�m using.

The regex you gave me looks like matches only the last folder name, I would like the regex matches the last 2 folder level.

I mean:

The regex you gave me matches this:

C/my user/documents/games & music

I would like the regex match last 2 folders as follow:

C/my user/documents/games & music

Thanks in advance again.

Ok.

sed 's/\(.*\/\).*\/[^/]*$/\1final folder/g' filename

Many thanks devtakh, it works. Many thanks posix either.

Sorry, my last question regarding this, I didn�t realize about this part before.

I can see your script works, but now I found that one or two directories contain only 2 subfolder levels.

How to insert an if statement within sed to match the last 2 folders if the entire string contains 3 subfolders and match only the very last folder if entire string cotainds only 2 subfolders?

I mean:

C/my user/documents/photos 09-24-2008 (If has 3 subfolders)
then match desired
C/my user/documents/photos 09-24-2008 (match last 2 subfolders)
C/my user/travels (If has 2 subfolders)
then match desired 
C/my user/travels (match only last subfolder)

Thanks again for any help.

sed 's/\([^/]*\/[^/]*\/\).*$/\1final folder/g' file

Thanks devtakh,

It works perfect!

Now I�m need to search and replace the same pattern in a file with more than one column.

I�m trying with awk and its gsub function, but the same regexp that works in SED, doesn�t seem to do anything in awk script.

What could be wrong?

I�m trying with this: (search and replace in column 3 only)

awk -F"|" '{gsub(/\([^/]*\/[^/]*\/\).*$/,"final folder",$3); print $0}' inputfile

Maybe somebody could help me with this.

Thanks in advance.

It will not work. Can you show how does you file looks like then I may be able to help you.

Of course devtakh,

A sample would be as follow:

A11|A12|C/my user/documents/games & music|A14
A21|A22|C/my user/documents/photos 09-24-2008|A24
A31|A32|C/my user/settings|A34

And desired result as follow:

A11|A12|C/my user/final folder|A14
A21|A22|C/my user/final folder|A24
A31|A32|C/my user/final folder|A34

The script I�ve tested is:

awk -F"|" -v OFS="|" '{gsub(/\([^/]*\/[^/]*\/\).*$/,"final folder",$3); print $0}' file

Many thanks for your help, really.

If you really want to use awk, then try this:

awk -F"|" '{f=index($3,"/");n=index(substr($3,f+1),"/");$3=substr($3,1,n+f)"final folder";print}' OFS="|" filename

cheers,
Devaraj Takhellambam

Really thanks devtakh. It works great!, exactly for what I need.

Many thanks again.:b: