Using Sed and Find with Grep Linux - sed

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

Related

how to use sed to replace string containing parenthesis

I am trying to use sed to replace the following but not working
replace datetime.now(pytz.utc) with datetime.utcnow() recursively
i have tried the following
grep -rl "datetime.now(pytz.utc)" . | xargs sed -i 's/datetime.now\(pytz.utc\)/datetime.utcnow\(\)/g'
mac command equivalent
LC_ALL=C
grep -e "datetime.now(pytz.utc)" -rl . | xargs sed -i '' 's/datetime.now\(pytz.utc\)/datetime.utcnow\(\)/g'
as you can see i tried to escape all the parentheses but does not work
anyone know how to properly use sed to replace datetime.now(pytz.utc) with datetime.utcnow()?
I tried to explain in the comments, but obviously I wasn't clear. Here are two potential solutions to your problem:
Using your 'grep/xargs' method:
grep -rl "datetime.now(pytz.utc)" . | xargs sed -i 's/datetime.now(pytz.utc)/datetime.utcnow()/g'
Using the 'find/exec' method:
find . -type f -exec sed -i 's/datetime.now(pytz.utc)/datetime.utcnow()/g' {} \;
Both options will replace "datetime.now(pytz.utc)" with "datetime.utcnow()" in the files found. Both answers are platform independent provided you have GNU sed, not BSD sed.

Find and replace using find and sed

This post shared how to remove html comments from a file at the command line.
sed -e :a -re 's/<!--.*?-->//g;/<!--/N;//ba' file.html
I'm trying to extend that to remove html comments from all files in a directory, but I'm having a hard time. Some of my attempts include:
find /my/folder/plus/subfolders -name "*.html" -exec "sed -e :a -re 's/<!--.*?-->//g;/<!--/N;//ba'"
And based on this, I've tried this approach too:
find /my/folder/plus/subfolders -name "*.html" -exec sed -i s/<!--.*?-->//g;/<!--/N;//ba {} +
Where am I going wrong?
You just needed to add the in place option -i and change the file to {}.
find /my/folder/plus/subfolders -name "*.html" -exec sed -i -e :a -re 's/<!--.*?-->//g;/<!--/N;//ba' {} +

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 all emails in files and replace with specific email

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?

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