Find all emails in files and replace with specific email - find

How can I find all emails in php files and replace with an email?
find . -iname '*.php' -exec grep -E -o "\b[a-zA-Z0-9.-_]+#[a-zA-Z0-9.-]+\.[a-zA-Z]+\b" -exec sed -i 's//email#domain.com/g' {} \;

Find emails in php files command is:
find . -iname '*.php' -exec grep -E -o "\b[a-zA-Z0-9.-_]+#[a-zA-Z0-9.-]+\.[a-zA-Z]+\b" {} \;
and replace command is:
find . -iname '*.php' -type f -exec sed -i 's/old-email#domain.com/new-email#domain.com/g' {} \;
But I don't know exactly how to join them in one command.
Any ideas?

Related

find -ctime bash alternative in Perl

Kind of new to Perl, still navigating my way through.
Is there another way to write the bash command below in "Perl"?
find $INPUT_DIR -ctime -$DAYS_NUM -type f -exec grep -hs EDI_DC {} \; |
grep -i -v xml >> $OUTPUT_DIR/$OUTPUT_FILENAME
where INPUT_DIR, DAYS_NUM, OUTPUT_DIR and OUTPUT_FILENAME are arguments passed during runtime.
When you try to convert find command to perl, consider using find2perl script.
It generate the perl code.
find2perl 'INPUT_DIR' -ctime -'DAYS_NUM' -type f -exec grep -hs EDI_DC {} \;

Find not matching a grep, after results

Hopefully this is a simple one, but I can't figure it out. I'm trying to work out why this command isn't finding any results:
find . -iname '*.cgi' -o -iname '*.txt' -o -iname '*.htm' -o -iname '*.html' -o -iname '*.php' -exec grep -l 'community.cgi' {} +
If I simplify it and just do:
find . -iname '*.cgi' -o -iname '*.txt' -o -iname '*.htm' -o -iname '*.html' -o -iname '*.php'
Then I get the list of files I'm expecting. For some reason the -exec part doesn't seem to be what I'm expecting. If I just run a basic grep on ALL files, I get the list of files as well:
grep -l 'community.cgi' .
Logical-and binds tighter than logical-or. Try:
find . \( -iname '*.cgi' -o -iname '*.txt' -o -iname '*.htm' -o -iname '*.html' -o -iname '*.php' \) -exec grep -l 'community.cgi' {} +
Here, parens (which have to be escaped to pass through the shell) are used to bind the -iname tests together so that -exec runs if any one of those tests is true.

GNU find and more complex -exec option parameter values

This works for me:
$> find . -name "*.log" -exec basename '{}' \;
20160114.log
20160115.log
20160116.log
20160117.log
20160118.log
Is the {}, \ and ';' mandatory when using -exec as any other syntax simply doesn't work?
The following, more complex example doesn't work:
$> find . -name "*.log" -exec echo $(basename '{}') \;
./log/20160114.log
./log/20160115.log
./log/20160116.log
./log/20160117.log
./log/20160118.log
echo here is just to demonstrate. I eventually plan to use something like rm $TARGET_DIR/$(basename '{}') in its placeā€¦ It just doesn't work that way (nesting). Any ideas?

Using Sed and Find with Grep Linux

I am writing a script that will saech for php files that contain a phrase and I would like that phrase replaced with a new one below is my little script but it is not working it searches ok but does not work with the search and replace section
find . -type f -name "*.php" -exec grep -H "define('DB_HOST', 'localhost');" {} \; | xargs sed -i "define('DB_HOST', 'localhost');/define('DB_HOST', '10.0.0.1');/g"
can someone explain to me what i am doing wrong
many thanks
Joe
did you forget the 's/' at the beggining of the sed expression? As in
sed 's/expression1/expression2/g'
You seem to have
sed 'espression1/expression2/g'
Edit
Another thing: You don't need to use xarg here. You can use multiple -exec flags - and it will to each only if all the previous succeeded:
find . -name '*.php' -exec grep 'whatever' {} \; -exec sed -i 's/whatever/you want/g' {} \;
This will work:
find . -type f -name "*.php" -exec grep -l "define('DB_HOST', 'localhost');" {} \; | xargs sed -i "s/define('DB_HOST', 'localhost');/define('DB_HOST', '10.0.0.1');/g"
Corrections
Missing s/ in sed search and replace command
use grep -l instead of grep -H

Can find tell me if no files exist?

On my FTP server, I look for files delivered in the past day and remove in-place header & trailer records.
find . -type f -name "CDC*" -ctime -1 -exec sed -i'' -e '1d' -e '$d' '{}' \;
This works well.
I want to automate this in a script. But how can I send myself an email notification is no files are found? I am thinking of doing something like:
find . -type f -name "CDC*" -ctime -1 -exec sed -i'' -e '1d' -e '$d' '{}' \;
EXIT=`echo $?`
case $EXIT in
0) ...do stuff...
*) mail....exit
esac;;
There has to a better way, right?
I'm pretty sure that you could take whatever command you need to do the search, and pipe a wc -l on to the end of it. Then use an if statement to check for zero. So using your example above.
NUMLINES=`find . -type f -name "CDC*" -ctime -1 -exec sed -i'' -e '1d' -e '$d' '{}' \ | wc -l`
if [ "$NUMLINES" -eq 0 ] ; then
foo
fi
Or something like that. I didn't check if that syntax is correct though. But i'm sure you get my drift