find & sed: remove lines - linux

I am trying to delete some line in PHP files. I tried to use an find, exec combination:
find . -name '*.php' -exec sed '/#category/d' {} \;
but it only prints out the files contents. Is there anythin wrong in the syntax? Or what is the problem?

Could you try this command:
find . -name '*.php' -exec sed -i '/#category/d' {} \;
I think you've missed -i option

It works, but probably not how you expect.
find . -name '*.php' -exec sed -i '/#category/d' {} \;
Will kill the lines in question.

This should be the command for sed so try to add -i :
sed -i ".bak" '/culpa/d' test.txt
find . -name '*.php' -exec sed -i '/#category/d' {} \;
Source of the answer:
Bash - find a keyword in a file and delete its line

Related

sed ack search / replace line break with string

I have looked into a few SO threads, non of which have helped my specific situation.
I am trying to update a PHP app that I took over from php 5.6 to php 8.0
With that said there are MANY instances that look like:
<?
echo ...
function
I need to find all cases where <? is followed directly by a newline and replace it with <?php(newline)
Per the SO posts I've read .. I think I am coming close with the following:
find ./ -type f -readable -writable -exec sed -i ":a;N;$!ba;s/\<\?\n/\<\?php\n/g" {} \;
I think I am close .. But I can't figure out why it won't replace <?\n with <?php\n as the sed statement works without the newline. But per THIS POST it looks like I am doing it correctly.
What am I doing wrong?
Iterations I've tried:
$ find ./ -type f -readable -writable -exec sed -i ":a;N;$!ba;s/\<\?\n/\<\?php\n/g" {} \;
$ find ./ -type f -readable -writable -exec sed -i ":a;N;$!ba;s/<\?\n/<\?php\n/g" {} \;
$ find ./ -type f -readable -writable -exec sed -i ":a;N;$!ba;s/<?\n/<?php\n/g" {} \;
$ find ./ -type f -readable -writable -exec sed -i ":a;N;$!ba;s/<?\n\r/<?php\n/g" {} \;
$ find ./ -type f -readable -writable -exec sed -i ":a;N;$!ba;s/<?\r\n/<?php\n/g" {} \;
The sed command itself could be something as simple as:
sed -i 's/<?$/<?php/'
Glue that together with find and it might work for you.
$ is an anchor matching the end of a line, you might consider using ^ to anchor the match to the beginning as well:
s/^<?$/<?php/

find and sed doesn't work in centos 7

I'm trying to find and replace a word in my entire project and I tried below versions of find and sed in centos 7 but nothing works.
find ./ -name "*.php" -exec sed -i '' s/mysql_/mysqli_/g \;
find ./ -name "*.php" -exec sed -i '' s/mysql_/mysqli_/g {} \;
find ./ -name "*.php" -exec sed -i '' 's/mysql_/mysqli_/g' {} \;
find ./ -name "*.php" -ls | xargs sed -i '' 's/mysql_/mysqli_/g'
sed: can't read s/mysql_/mysqli_/g: No such file or directory
All above commands giving me this error in loop even though I'm running these commands from the root of my project. Permissions are all correct. If I simply use find command alone it's working
find ./ -name "*.php" -ls (This Works)
I tried solutions available in stackoverflow but nothing works.
The fist pair of quotes in sed aren't necessary, try:
find ./ -name "*.php" -exec sed -i s/mysql_/mysqli_/g {} \;
The syntax is either -i'prefix' or --in-place='prefix', not -i 'prefix', since you added an space between the prefix and the argument, it's making sed use the prefix (empty string) argument as the regex and use the actual regex as a filename argument, which obviously won't find.
That's why you are getting the can't read s/mysql_/mysqli_/g: No such file or directory error.

How to remove specific lines from all files?

I want to delete all lines begin with 'sometext' from many files:
find . -name "*.php"|xargs -I {} sed -e '/^sometext/d' {}
But this put me output to console. How to modify this files directly?
Use -i option of sed:
sed -i -e '/^sometext/d' file
Tell sed to modify the files "in place":
find . -name "*.php" | xargs sed -i '' -e '/^sometext/d'
Note that the blank '' after -i is required, otherwise a new copy with a default suffix will be created.
Also note the pruning if your unnecessary -I in xaegs
You can accomplish this with exec:
find . -name "*.php" -exec sed -i '/^sometext/d' {} \;

Linux find and replace

How can I replace "abc" with "abcd" on all files of a folder using shell?
Is it possible using sed command?
Try the following command for the file file.txt:
sed -i 's/abc/abcd/g' file.txt
Try the following command for all files in the current folder:
find . -maxdepth 1 -type f -exec sed -i 's/abc/abcd/g' {} \;
For the files in the current directory and all subdirectories:
find . -type f -exec sed -i 's/abc/abcd/g' {} \;
Or if you are fan of xargs:
find . -type f | xargs -I {} sed -i 's/abc/abcd/g' {}
sed -i 's/abc/&d/g' *
should work.
Yes:
find /the/folder -type f -exec sed -i 's,\<abc\>,&d,g' {} \;

search multiple php files, and replace text?

i have several php files, and there's references to .html files in those.
I need to replace ".html" to ".php"
how can i do this in bash ?
for file in $(find . -name "*.php"); do
sed "s/\.html/.php/g" $file > $$ && mv $$ $file
done
find -name '*.php' -exec sed -ie 's:.html:.php:g' {} \;
Try sed:
find -name "filenamepattern.php" -print0 | xargs -0 sed 's/\.html/\.php/g'
GNU find
find /path -type f -iname '*.php' -exec sed -i.bak 's/\.html/\.php/g' {} +;

Resources