Strip passwords from text file?

michaelsanford

Translator, Web Developer
I've got a nice little one-liner that I use to copy all .php files in the cwd to .phps files so my co-workers can see them.

Despite the fact that I have the folder password protected, I'd really like to be able to strip the passwords from the .phps files automatically. It also makes it easier to print the code (coloured from a web browser) without having to manually obliterate them with a pen... Passwords appear in things like mysql_connect() calls.

The password is a unique string that does not occur anywhere else in the script (that is, it's not a common word I need to be worried about removing only certain instances of).

So essentially I just need to search the contents of a plain-text file word by word, and if it finds this password string, replace it.

Any ideas? I don't know any PERL :p

Thanks!
 
Here, try this:

cat phps_file | sed 's/password_string//g' > new.phps file

After that, you can either rename the new phps file to the old name or just delete the old file. sed will go through and replace the password string with nothing (if you want it replaced with anything, place something between the two slashes (//) before the g).
 
I've managed to get it working this $ sed /password/s//--/ form.phps (it seems quicker than using it piped with cat). I've verified it with output to stdout (screen).

With two problems:
1) When I try to redirect it to a file of the same name (whether or not I use a piped cat) it just outputs a blank file.

2) I need to find a way to do this with wildcards, since I want to make it part of the one-liner than makes the phps files in the first place, but I'm having problems passing wildcards to sed for some reason.

I've tried using $ find . -name "*.phps" -exec sed /password/s//--/ > '{}' \; but it just takes the prompt away and does seemingly nothing until I terminate it (CTRL-C).

Any ideas?
 
Redirection to the same file name won't work because it's replacing the file and then trying to read from it - so it'll end up as an empty file. You'll have to make a temp file, unfortunately.

Wildcards can be a pain to get working right. If you don't mind post-processing the files after you've made them, then here's a little shell script that can work. I'm going to go under the assumption that all the files are in the same directory here....

I'm going back with cat too...with find it just appears to hang.
Code:
#!/bin/sh

cd dir_where_php_files_are

for file in `ls`; do
    cat $file | sed 's/password//g' > temp.phps
    mv temp.phps $file
done

That'll go through all the files in the directory, strip the password string out of the file (if it's there), save the file to a temp file, then move the temp file back to the original name. Speed will depend on how many files are in the directory.

Note that this won't mess any files up that don't have the password string, but they will also be processed through sed, saved to a temp file, then moved back to the original file name. It doesn't take very much time, but if you happen to know which files will have a password string and which don't, you may want to separate them so that the script runs a little quicker.
 
Awesome thanks! :cool: I've integrated that with the script that makes phps files so it all works seemlessly.

Just looking at the script, won't for files in `ls` truncate the password from the php files as well? I added for files in `ls *.phps` just in case.
 
Back
Top