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

It would be better if you provide some example and expected outcomes.
That will help us to understand the requirement clearly.

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
1 Like

In my file.php
I have following;
action='index.html?
and I want to replace it with;
action='<website_name>/index.html?

Replace sed command in previous script as below

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