Solved

shell script to recursively search and find/replace text in target files

Posted on 2011-02-16
7
528 Views
Last Modified: 2012-05-11
Hello All,

This is probaably a 1 minute solution for many script maseters, but for me is quite muddy.

Task:
1. Recursively search  all child directories for file WP-CONFIG.PHP
2. Find all "Lines" that begin with define('DB_PASSWORD', 'xxxxxx');   -- note 'xxxxxx' can be any length text.
3. Replace the 'xxxxxxx' text with new text that I would supply as paramter argument upon running the script.
4. Produce a count of the number of times the script found and editied a WP-CONFIG.PHP file.

Thats it.
0
Comment
Question by:Louis Capece
7 Comments
 
LVL 6

Expert Comment

by:de2Zotjes
ID: 34907589
#!/usr/bin/env bash

REPLACE=$1
n=0
for entry in $(find . -name WP-CONFIG.PHP -a -type f); do
    n=$(($n+1))
    sed -e "s/^define\('DB_PASSWORD','[^']*'/define('DB_PASSWORD','$REPLACE'/" -i $entry
done

echo $n

Open in new window


Line 3 takes the first command line argument and puts it in the REPLACE var
Line 5 uses the find utility to locate every file named WP-CONFIG.PHP under the current dir
Line 7 replaces every occurence of ....DB_PASSWORD with the text in REPLACE var
 

0
 
LVL 4

Expert Comment

by:florjan
ID: 34907612
find . -iname "WP-CONFIG.PHP" -print | xargs sed -i 's/oldpass/newpass/g'
grep -lr "newpas" .

Open in new window

Make sure to escape(backslash\ any special characters) and to tell me if there is any other line same as password so I can adapt the script/
0
 
LVL 31

Expert Comment

by:farzanj
ID: 34907632
1.  ls -R WP-CONFIG.PHP
or  find . -type f -name WP-CONFIG.PHP
2. grep -R '^define(\'DB_PASSWORD\''

3.  sed "s/xxxxx/yyyyy/g" $1
4.  #Before modifying the script
     Precount=$(grep -c "XXXX" WP-CONFIG.PHP)
     #Edit using above
     Postcount=$(grep -c "XXXX" WP-CONFIG.PHP)
     Edited=$(( Precount - Postcount ))
0
Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

 

Author Comment

by:Louis Capece
ID: 34909944
Could someone please confirm this, I suspect there may be confusion...
the existing password is NOT known, it could be anything that is beween the single quotes.

In a nutshell, what I an trying to do is alter all the WP_CONFIG.PHP "Define Password" statement to a single value....the existing passwords are all different.
0
 
LVL 11

Expert Comment

by:tel2
ID: 34910642
Hi louiscapece,

de2Zotjes's solution seems to cater for any existing password.
But I don't think it caters for the space you have between the comma and the 'xxxxxx' (old password).  To make it flexible in this regard (i.e. to handle 0 or more spaces there), maybe change line 7 to something like this:
    sed -e "s/^define\('DB_PASSWORD', *'[^']*'/define('DB_PASSWORD', '$REPLACE'/" -i $entry
0
 
LVL 4

Accepted Solution

by:
florjan earned 500 total points
ID: 34910645
find . -iname "WP-CONFIG.PHP" -print | xargs sed -i "s/('DB_PASSWORD', '.*')/('DB_PASSWORD', 'newpassword')/g"
grep -lr "newpas" .

Open in new window

0
 
LVL 4

Expert Comment

by:florjan
ID: 34910669
I have tested script from my last post on gentoo on my test wp install in bash and it worked.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I am a long time windows user and for me it is normal to have spaces in directory and file names. Changing to Linux I found myself frustrated when I moved my windows data over to my new Linux computer. The problem occurs when at the command line.…
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now