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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting To replace a string in file by its parent folder name.
# 1  
Old 12-10-2010
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 script should grep for different website's folders name each time.Apart from "website_name" folder all the entities are common in mentioned directory/file structure.
I will appreciate if anyone will help me in getting through this.
Thanx in advance.

Arien
# 2  
Old 12-10-2010
It would be better if you provide some example and expected outcomes.
That will help us to understand the requirement clearly.
# 3  
Old 12-10-2010
Code:
for file in `find /folder1 -type f -name "file.php" | xargs`     # This will list ot the files and if file name is not static then use "*.php"
do
    website=`echo "$file" | cut -d"\/" -f3`               # Extract website name
    sed -e 's/string/$website/g' $file                      # Substitute particular string with website name
done

This User Gave Thanks to For This Post:
R0H0N
# 4  
Old 12-10-2010
In my file.php
I have following;
action='index.html?
and I want to replace it with;
action='<website_name>/index.html?
# 5  
Old 12-10-2010
Quote:
Originally Posted by R0H0N
Code:
for file in `find /folder1 -type f -name "file.php" | xargs`     # This will list ot the files and if file name is not static then use "*.php"
do
    website=`echo "$file" | cut -d"\/" -f3`               # Extract website name
    sed -e 's/string/$website/g' $file                      # Substitute particular string with website name
done

Replace sed command in previous script as below

Code:
sed -e "s/action='index/action='$website\/index/g" $file

R0H0N
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 of a file with a string of another file for matches using grep,sed,awk

I have a file comp.pkglist which mention package version and release . In 'version change' and 'release change' line there are two versions 'old' and 'new' Version Change: --> Release Change: --> cat comp.pkglist Package list: nss-util-devel-3.28.4-1.el6_9.x86_64 Version Change: 3.28.4 -->... (1 Reply)
Discussion started by: Paras Pandey
1 Replies

2. Shell Programming and Scripting

Request for Shell script to move files from Subfolder to Parent folder and delete sub folder

Hi Team, I am new to shell script and there is a requirement where files should be moved from Subfolder to parent folder. Eg: parent folder --> /Interface/data/test/IN Sub folder -->/Interface/data/test/IN/Invoice20180607233338 Subfolder will be always with timestamp... (6 Replies)
Discussion started by: srivarun15
6 Replies

3. Shell Programming and Scripting

Move parent folder if contents match a parameter

Hi all, Hoping someone can help. I'm looking to be able to search a particular folder path for folders that contain certain files / formats and then move the parent folder. eg. /foo/bar/folder1/file.exe /foo/bar/folder2/file.exe and then move the folder1/2 tp another folder. So if... (1 Reply)
Discussion started by: springs2
1 Replies

4. UNIX for Dummies Questions & Answers

Grep with parent folder

Hi, When I do ls -l | grep -h STRING I need not only to get those files, but also the parent folder of those files, no idea how do that. BTW the names of those parent folders are never longer than 3 digits, if that helps. Any assistance most welcome, /bw, OmarKN Please... (9 Replies)
Discussion started by: OmarKN
9 Replies

5. Shell Programming and Scripting

replace (sed?) a single line/string in file with multiple lines (string) from another file??

Can someone tell me how I can do this? e.g: Say file1.txt contains: today is monday the 22 of NOVEMBER 2010 and file2.txt contains: the 11th month of How do i replace the word NOVEMBER with (5 Replies)
Discussion started by: tuathan
5 Replies

6. Shell Programming and Scripting

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... (11 Replies)
Discussion started by: cgkmal
11 Replies

7. Shell Programming and Scripting

How to read a subfolder one by one in parent folder?

Hi friends, I am getting some trubles in folder reading. I am having 10 subfolders inside server7 folder. i wanna to read a subfolder name first and check if the foldername gets started with "http". if so , i need to read a file inside that folder. This willl continue for... (1 Reply)
Discussion started by: kamatchirajan
1 Replies

8. Shell Programming and Scripting

full path of a file situated either in parent's dir. or parent's parent dir. so on...

hi experts(novice people can stay away as it is no child's game), i am developing a script which works like recycle bin of windows. the problem i am facing is that when ever i am trying to delete a file which is situated in parent directory or parent's parent directory i am unable to... (1 Reply)
Discussion started by: yahoo!
1 Replies

9. 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

10. UNIX for Dummies Questions & Answers

How do I search parent folder only?

I have a directory /dir1/dir2/dir3/dir4/dir5. How can I search for files only in dir4/ and not in dir5? I tried the maxdepth command, but it doesn't work in this flavor of sun OS. When I search for files in dir4, it's giving me results from dir5 as well. I don't want this. Thanks. (3 Replies)
Discussion started by: bbbngowc
3 Replies
Login or Register to Ask a Question