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
# 8  
Old 04-23-2010
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)


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

Maybe somebody could help me with this.

Thanks in advance.
# 9  
Old 04-23-2010
It will not work. Can you show how does you file looks like then I may be able to help you.
# 10  
Old 04-23-2010
Of course devtakh,

A sample would be as follow:


Code:
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:
Code:
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:
Code:
awk -F"|" -v OFS="|" '{gsub(/\([^/]*\/[^/]*\/\).*$/,"final folder",$3); print $0}' file

Many thanks for your help, really.
# 11  
Old 04-23-2010
If you really want to use awk, then try this:

Code:
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
# 12  
Old 04-23-2010
Really thanks devtakh. It works great!, exactly for what I need.

Many thanks again.
Smilie
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