Replace last 2 folder directory string with sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace last 2 folder directory string with sed
# 1  
Old 04-22-2010
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:

Code:
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
Code:
sed 's/\/documents\/games & music/\/final folder/g' infile

Desired result is as follow:
Code:
C/my user/final folder
C/my user/final folder
C/my user/final folder

Any help would be very appreciated.
# 2  
Old 04-22-2010
use

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


cheers,
Devaraj Takhellambam
# 3  
Old 04-22-2010
you can try this..but Smilie
Code:
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???Smilie
# 4  
Old 04-22-2010
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:

Code:
C/my user/documents/games & music

I would like the regex match last 2 folders as follow:
Code:
C/my user/documents/games & music

Thanks in advance again.
# 5  
Old 04-22-2010
Ok.

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

# 6  
Old 04-22-2010
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:

Code:
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)

Code:
C/my user/travels (If has 2 subfolders)
then match desired 
C/my user/travels (match only last subfolder)

Thanks again for any help.
# 7  
Old 04-22-2010
Code:
sed 's/\([^/]*\/[^/]*\/\).*$/\1final folder/g' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace string in XML file with awk/sed with string from another

Sorry for the long/weird title but I'm stuck on a problem I have. I have this XML file: </member> <member> <name>TransactionID</name> <value><string>123456789123456</string></value> </member> <member> <name>Number</name> ... (9 Replies)
Discussion started by: cozzin
9 Replies

2. Shell Programming and Scripting

Replace character in files of entire folder? sed? or what?

Hello, I do have several files in one folder each file contains measurement data. for each file I would like to replace the character "," by "." ? How can I do this and how can I do this for each file at once? E.G. data_1.dat, data_x.dat (original version) data_1out.dat, data_x_out.dat... (10 Replies)
Discussion started by: rollinator
10 Replies

3. Shell Programming and Scripting

HPUX find string in directory and filetype and replace string

Hi, Here's my dilemma. I need to replace the string Sept_2012 to Oct_2012 in all *config.py files within the current directory and below directories Is this possible? Also I am trying to find all instances of the string Sept_2012 within files in the current directory and below I have... (13 Replies)
Discussion started by: pure_jax
13 Replies

4. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

5. Shell Programming and Scripting

replace (sed?) a string in file with multiple lines (string) from variable

Can someone tell me how I can do this? e.g: a=$(echo -e wert trewt ertert ertert ertert erttert erterte rterter tertertert ert) How do i replace the STRING with $a? I try this: sed -i 's/STRING/'"$a"'/g' filename.ext but this don' t work (2 Replies)
Discussion started by: jforce
2 Replies

6. Shell Programming and Scripting

To replace a string in file by its parent folder name.

Hi all, I have a directory structure like "folder1/folder2/website_name/folder3/folder4/file.php." I need to write a script which will enter into file.php and replace a particular string of characters with "website_name" folder name. In folder2,there are many such website's folders kept.So... (4 Replies)
Discussion started by: arien15488
4 Replies

7. Shell Programming and Scripting

How to use sed to replace the a string in the same file using sed?

How do i replace a string using sed into the same file without creating a intermediate file? (7 Replies)
Discussion started by: gomes1333
7 Replies

8. Shell Programming and Scripting

Using sed to replace a string in file with a string in a variable that contains spaces

Hi, i call my shell like: my_shell "my project name" my script: #!/bin/bash -vx projectname=$1 sed s/'PROJECT_NAME ='/'PROJECT_NAME = '$projectname/ <test_config_doxy >temp cp temp test_config_doxy the following error occurres: sed s/'PROJECT_NAME ... (2 Replies)
Discussion started by: vivelafete
2 Replies

9. Shell Programming and Scripting

sed: replace string with another string (with spaces)

Hi I have an XML file with strings XABCD, XEFGHX and XIJKLX. I would like to replace XABCDX with "This is the first string", XEFGHX with "This is the second string" and XIJKLX with "This is the third string". What is the best way to implement this? Should I have a file with the data that is... (4 Replies)
Discussion started by: zmfcat1
4 Replies

10. Shell Programming and Scripting

Replace string in all files in a folder and subfolders.

i need to change string in all files in current folder and all subfolders. i wrote the following script. It works good except it dont delete temp file from subfolders. for z in `find . -type f -name "*.html" -o -name "*.htm"`; do sed -e 's@abc@xyz@g' $z>temp; mv temp $z; done any idea?... (1 Reply)
Discussion started by: crazynups
1 Replies
Login or Register to Ask a Question